Jquery getJSON到外部PHP页面

Pma*_*oen 9 php ajax jquery json getjson

我一直在尝试向外部服务器发出ajax请求.到目前为止,我已经了解到,由于安全原因,我需要使用getJSON来执行此操作?

现在,我似乎无法对外部页面进行简单的调用.我试图尽可能地简化它,但它仍然无法正常工作.我有2个文件,test.html和test.php

我的test.html这样打电话给localhost进行测试:

    $.getJSON("http://localhost/OutVoice/services/test.php", function(json){
    alert("JSON Data: " + json);
});
Run Code Online (Sandbox Code Playgroud)

我希望我的test.php返回一个简单的'测试':

$results = "test";
echo json_encode($results);
Run Code Online (Sandbox Code Playgroud)

我可能会犯一些令人难以置信的菜鸟错误,但我似乎无法弄明白.此外,如果这有效,我怎样才能将数据发送到我的test.php页面,就像test.php?id = 15一样?


test.html页面调用localhost上的test.php页面,同一目录我没有收到任何错误,只是没有警告..

mof*_*off 16

可能是你没有在test.php的回调.另外,json_encode只接受一个数组:

$results = array("key" => "value");
echo $_GET['callback'] . '(' . json_encode($results) . ')';
// the callback stuff is only needed if you're requesting from different domains
Run Code Online (Sandbox Code Playgroud)

XMLHttpRequest当你使用时,jQuery会自动切换到JSONP(即使用脚本标签代替)http://.如果在同一个域上有test.html和test.php,请尝试使用相对路径(并且不使用回调).

  • 这完全打开了你的XSS可敬性 - http://www.metaltoad.com/blog/using-jsonp-safely (2认同)

dre*_*ish 15

小心moff的回答.它有一个常见的XSS漏洞:http://www.metaltoad.com/blog/using-jsonp-safely