JSONP请求PHP页面无法正常工作(跨域) [英] JSONP request to PHP page not working (cross domain)
问题描述
这是我的PHP页面(在不同的URL上)...
This is my PHP page (on a different URL)...
<?php
header('Content-Type: application/json');
?>
stat({"online":1});
这是我的jQuery:
And this is my jQuery:
$(document).ready(function(){
var url = 'http://blah.com/jsontest.php?callback=stat';
$.getJSON(url, function(data) {
if (data.online !== undefined) {
console.log('yay');
}
}).error(function() {
console.log('no');
});
});
由于某种原因,它总是记录'no',即它运行错误函数。
For some reason it is always logging 'no' i.e. its running the error function.
使用jQuery 1.5.2的任何想法?
Using jQuery 1.5.2 any ideas?
推荐答案
首先,JSON-P是不是JSON。内容类型应为 application / javascript
。有些浏览器可能会因为不安全而拒绝JSON-P作为JSON。
First, JSON-P is not JSON. The content type should be application/javascript
. Some browsers may reject JSON-P served as JSON for being unsafe.
其次,getJSON期望您请求的网址为?
用于回调方法名称(你需要让你的PHP注意 $ _ GET ['callback']
)。
Second, getJSON expects that the URL you request to have a ?
for the callback method name (and you'll need to get your PHP to pay attention to $_GET['callback']
).
第三,如果修复不起作用,请查看Firebug / Chrome调试器/ Dragonfly /等中的Net选项卡,看看实际上有哪些数据通过网络。
Third, if fixing that doesn't work, look at the Net tab in Firebug / Chrome debugger / Dragonfly / etc and see what data is actually going across the wire.
这篇关于JSONP请求PHP页面无法正常工作(跨域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!