当使用JSON和使用jQuery $。阿贾克斯JSONP是什么时候? [英] When to use json and when jsonp with jquery $.ajax?
问题描述
在我们的网站,有些是SSL和一些非SSL。
In our site, Some pages are SSL and some are non-SSL.
例如:
http://www.example.com/search/patients
https://www.example.com/patients
现在我正在寻找病人 http://www.example.com/search/patients
页面和发送服务器请求的https:/ /www.example.com/patients
通过 jQuery的$就
功能的dataType = JSON
。我无法获取数据。
Now I am searching patients on http://www.example.com/search/patients
page and send server request to https://www.example.com/patients
via jQuery $.ajax
function and dataType=json
. I am unable to get data.
问题:
-
我应该使用
JSONP
当我们从HTTP
请求HTTPS
或HTTPS
到HTTP
同一台服务器上?
Should I use
jsonp
when we request fromhttp
tohttps
orhttps
tohttp
on same server?
如果我使用SSL两个网址,然后将其与正常工作的dataType = JSON
仅
If I use SSL for both URLs then will it work with dataType=json
only
感谢
推荐答案
由于同源策略与Ajax请求只允许使用: 域名,应用层协议,以及(在大多数浏览器)端口 运行脚本的HTML文档的数目是相同的
Due to Same Origin Policy your ajax request is allowed only if: domain name, application layer protocol, and (in most browsers) port number of the HTML document running the script are the same
你的情况的应用层协议是不同的,这就是为什么你的脚本失败
可能的解决方案是:
-
JSONP ,该要由服务器提供
CORS ,这是一个更优雅和干净的解决方案,但还没有完全通过IE浏览器支持(IE7不支持它,IE8有一些局限性)
CORS, which is a more 'elegant' and clean solution, but is not yet fully supported by IE (IE7 doesn't support it, IE8 has some limitations)
这篇关于当使用JSON和使用jQuery $。阿贾克斯JSONP是什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!