【发布时间】:2017-03-14 04:47:46
【问题描述】:
我正在尝试使用jsonp 或XMLHttpRequest 和GET 方法访问cross-domain 数据。我的代码:
XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.com/ajax.php?code=BSE", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
alert(xhr.responseText);
}
xhr.send();
JsonP
$.ajax({
type: 'GET',
url: "http://example.com/ajax.php?code=BSE",
dataType: "jsonp",
jsonpCallback: "jsonp_callback",
crossDomain: true,
success: function(res){
console.log(res);
}
});
两种方法具有相同的行为。每当我发送请求时,它只会继续加载(即使我不确定它是否发送请求)并且什么也不做。
还有我的php代码:
PHP 代码:
header('content-type: application/json; charset=utf-8');
$dts=array('value'=>'123');
echo $_GET['jsonp_callback'] . '('.json_encode($dts).')';
【问题讨论】:
-
是的..所有其他jquery函数都工作正常..
-
同样的
XMLHttpRequest在 chrome 扩展中完美运行.. -
jsonpCallback: "jsonp_callback",这个回调函数真的存在吗 -
我希望以您的名义进行的编辑修正了您的错字,因为现在 XHR 代码有效
-
@Alive to Die:我对
jsonp真的了解不多
标签: javascript jquery ajax firefox firefox-addon