我有两个站点:
- https://a.site.com
- https://b.site.com
两个站点使用相同的通配符SSL证书
https://a.site.com/a.php:部分
$(document).ready(function() {
var tURL = "https://b.site.com/b.php?i=1";
$.ajax({
type: "GET",
url: tURL,
async: false,
contentType: "application/json",
dataType: 'jsonp', // Notice! JSONP <-- P (lowercase)
success:function(json){
alert("Success");
},
error:function(){
alert("Error");
}
});
})
在https://b.site.com/b.php我有:
//A query that returns a single value
$u = array('id' => $valueFromSQLQuery);
header('Content-type: application/json');
echo json_encode($u);
//returns {"id":630115} -- "id" is always the same, the number changes
当我运行https://b.site.com/b.php时,我得到{"id":630115}
显示在我的浏览器中,正如我所期望的。
当我运行https://a.site.com/a.php时,我得到SyntaxError: missing ; before statement Line 1 {"id":630116}
与firebug中的箭头指向:在{"id":630116}
我做错了什么?据我所知,jsonp是跨域执行ajax请求的正确方法。这是因为SSL还是我遗漏了什么?
@利文斯顿的回答给我指明了正确的方向。最后的结果是:
https://b.site/b.php更改为:
//A query that returns a single value
header('Content-type: application/json');
echo $_GET['callback'] . '(' . "{'id' : $valueFromSQLQuery}" . ')';
https://a.site.com/a.php改为
$(document).ready(function() {
var tURL = "https://b.site.com/b.php?callback=callback&i=1";
不知道实际返回的是什么"return something like"和实际返回是不同的。我假设你实际上正在生成一个JSON
响应,而不是一个JSONP
响应,需要在其中有回调(即,代码不仅仅是json数据)。
看到这个:简单的jQuery, PHP和JSONP的例子?