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.

    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作