jQuery的的getJSON外部PHP页面 [英] Jquery getJSON to external PHP page
问题描述
我一直在试图让一个Ajax请求到外部服务器。 我已经学会为止,我需要使用的getJSON这样做,因为安全原因呢?
I've been trying to make an ajax request to an external server. I've learned so far that I need to use getJSON to do this because of security reasons ?
现在,我似乎无法作出一个外部页面的简单调用。 我试图简化下来,就像我可以,但它仍然没有工作。 我有2个文件,test.html的&放大器; test.php的
Now, I can't seem to make a simple call to an external page. I've tried to simplify it down as much as I can but it's still not working. I have 2 files, test.html & test.php
我的test.html做这样的呼吁,到本地主机来进行测试:
my test.html makes a call like this, to localhost for testing :
$.getJSON("http://localhost/OutVoice/services/test.php", function(json){
alert("JSON Data: " + json);
});
和我想我的test.php返回一个简单的测试:
and I want my test.php to return a simple 'test' :
$results = "test";
echo json_encode($results);
我可能做一些令人难以置信的新手的错误,但我似乎无法弄清楚。 另外,如果这个工程,我怎么能发送数据到我的test.php的页面,就像你会做这样的test.php?ID = 15?
I'm probably making some incredible rookie mistake but I can't seem to figure it out. Also, if this works, how can I send data to my test.php page, like you would do like test.php?id=15 ?
该test.html的网页呼吁本地主机的test.php的页面,同一个目录 我没有得到任何错误,只是没有警报。
The test.html page is calling the test.php page on localhost, same directory I don't get any errors, just no alert ..
推荐答案
它的可以的是,你没有在test.php的回调。此外, json_en code
只接受一个数组:
It could be that you haven't got a callback in test.php. Also, json_encode
only accepts an array:
$results = array("key" => "value");
echo $_GET['callback'] . '(' . json_encode($results) . ')';
// the callback stuff is only needed if you're requesting from different domains
jQuery将自动切换到JSONP(即使用脚本标记而不是 XMLHtt prequest
)当您使用的http://
。如果你有test.html中,并test.php的同一个域,请尝试使用相对路径(没有回调)。
jQuery automatically switches to JSONP (i.e. using script tags instead of XMLHttpRequest
) when you use http://
. If you have test.html and test.php on the same domain, try using relative paths (and no callbacks).
这篇关于jQuery的的getJSON外部PHP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!