weixin_33720956 2015-12-22 00:37 采纳率: 0%
浏览 26

提取Json响应

I am trying to to extract a Json response in jquery sent from a php file. This is the .js code:

    $.ajax({
 url: 'index.php?page=register', //This is the current doc
 type: 'POST',
 datatype: 'json',
 data: {'userCheck': username},
 success: function(data){
    // Check if username is available or not
 },
 error: function(){
    alert('Much wrong, such sad');
 }
});

This is the response from the php file:

    if($sth->fetchColumn()!=0){
        //$response = array("taken");
        $response = array("username"=>"taken");
        echo json_encode($response);
        //echo '{"username':'taken"}';
    }else{
        //$response = array("available");
        $response = array("username"=>"available");
        echo json_encode($response);
        //echo '{"username":"available"}';
    }

I have tried all combinations I can think of in both files, but nothing seems to work. It is a simple check for a username in the database. If I console log the data I get from the response, I get this:

    {"username":"available"}<!DOCTYPE html>
    // The rest of the page html

So the info is there, but how do I access it? I have tried several syntaxes found around the internet, but no luck so far. I seem to recall that a json response only can contain valid json, so is the problem the html? I don't think I can avoid this due to the structure of my application, so hopefully it is possible to access the json with my present structure.

  • 写回答

3条回答 默认 最新

  • weixin_33730836 2015-12-22 00:48
    关注

    in you Ajax

    EDIT:

    change

    datatype:"json",
    

    the case of parameter name was not respected, the t must be T

    dataType:"json",
    

    now retry please

    $.ajax
    ({
        url: 'index.php?page=register', //This is the current doc
        type: 'POST',
        dataType: 'json',
        data: {'userCheck': username},
        success: function(data)
        {
            // Check if username is available or not
            switch(data.username)
            {
                case "available":
                    // do you want
                    break;
                case "taken":
                    // do you want
                    break;
            }
        },
        error: function()
        {
            alert('Much wrong, such sad');
        }
    });
    

    in PHP

    simply that, and don't forget to exit; to avoid include html page in your json response ! This is the code coming after the }".... who break your json output and make it unreadable by javascript (worste, it simply break your javascript !)

    echo json_encode(["username"=> ($sth->fetchColumn()!=0) ? "taken":"available"]);
    exit;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败