helloxielan 2014-10-03 14:56 采纳率: 0%
浏览 23

AJAX URL问题…很奇怪

I have a page from which I am trying to make an AJAX call, but it isn't working, and I'm stumped as to why. My call is:

$.ajax({
    type: "GET",
    url: "<relative URL>/index.php?action=x",
    dataType: "JSON"
}).success(function(person) {
    alert(person.name) //alerts naem
});
    return false;
});

and the PHP is:

<? 

if($_GET["action"] == "x"){

    $person = array("name"=>"Jon Doe","Reputation"=>"Good");
    header("Content-Type: application/json");
    echo json_encode($person);
}

?>

I don't THINK the issue is a faulty URL, since it is a c/p of working calls. It seems to me (but I'm not sure) that it chooses to reuse the old 'action-values' from when the source page was loaded originally. For some reason it completely ignores my action-value…?

Any ideas?

  • 写回答

3条回答 默认 最新

  • weixin_33690963 2014-10-03 15:10
    关注

    Try moving the PHP into its own file rather than the index.php, which I assume is your main PHP file.

    $.ajax({
        type: "GET",
        url: "/ajax.php?action=x",
        dataType: "JSON",
        success: function (person) {
           console.log(person.name);
        }
    });
    

    This should be a separate file. Call it ajax.php for an example.

    <? 
    // ajax.php
    if($_GET["action"] == "x"){
    
        $person = array("name"=>"Jon Doe","Reputation"=>"Good");
        die(json_encode($person));
    }
    
    ?>
    

    Just make sure ajax.php is in the route of your project and this should work. Check the console.log if it doesn't.

    评论
  • weixin_33709219 2014-10-03 15:34
    关注

    Try moving the variable into 'data':

    $.ajax({
        type: "GET",
        url: "<relative URL>/index.php", 
        data: "action=x",
        success: function (person) {
           console.log(person.name);
        }
    });
    
    评论
  • 乱世@小熊 2014-10-03 17:32
    关注

    Since you are calling index.php with a get parameter, you might be echoing the json_encode and then continuing to output everything else after the if statement.

    Try this?

    <? 
    
    if($_GET["action"] == "x"){
    
        $person = array("name"=>"Jon Doe","Reputation"=>"Good");
        header("Content-Type: application/json");
        echo json_encode($person);
        exit;
    
    }
    
    ?>
    

    I'm assuming you have more content in the index.php below that if statement. If so, the exit; statement would be needed so that you only echo the json and not anything after the if statement.

    The ajax response can be viewed in Google Chrome's Dev Tools, under the Network tab.


    Let's say you had no exit; statement:

    <? 
    
    if($_GET["action"] == "x"){
    
        $person = array("name"=>"Jon Doe","Reputation"=>"Good");
        header("Content-Type: application/json");
        echo json_encode($person);
    
    }
    
    ?>
    
    <html>
    <head></head>
    <body>
    ...etc, etc
    

    The ajax response under Dev tools would look something like this:

    {"name":"Jon Doe","Reputation":"Good"}<html><head></head><body>... and so on

    which would then try to be parsed by the $.ajax into a JSON object.

    But this is not possible because <html><head></head><body>... is not valid JSON.

    Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 存储过程或函数中的结果集类型变量如何使用。
  • ¥80 关于海信电视聚好看安装应用的问题
  • ¥15 vue引入sdk后的回调问题
  • ¥15 求一个智能家居控制的代码
  • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
  • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
  • ¥20 求各位能用我能理解的话回答超级简单的一些问题
  • ¥15 yolov5双目识别输出坐标代码报错
  • ¥15 这个代码有什么语法错误
  • ¥15 给予STM32按键中断与串口通信