gpt4 book ai didi

php - 在两个 https 站点之间打开 jQuery $.ajax 连接的正确方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 15:00:32 25 4
gpt4 key购买 nike

我有两个网站:

  1. > https://a.site.com
  2. > 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 ;在语句第 1 行 {"id":630116} 之前,firebug 中的箭头指向 {"id":630116}

中的:

我做错了什么?根据我的阅读,jsonp 是跨域执行 ajax 请求的正确方法。这是因为 SSL 还是我遗漏了什么?

更新@DrLivingston 的回答为我指明了正确的方向。最终结果是:

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";

最佳答案

没有看到实际返回的内容“返回类似的东西”和实际返回是不同的。我假设您实际上生成的是 JSON 响应,而不是 JSONP 响应,它需要在其中包含回调(即,代码不仅仅是 json 数据)。

看这个: Simple jQuery, PHP and JSONP example?

关于php - 在两个 https 站点之间打开 jQuery $.ajax 连接的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22725304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com