当使用JSON和使用jQuery $。阿贾克斯JSONP是什么时候? [英] When to use json and when jsonp with jquery $.ajax?

查看:113
本文介绍了当使用JSON和使用jQuery $。阿贾克斯JSONP是什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的网站,有些是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.

问题:

  1. 我应该使用 JSONP 当我们从 HTTP 请求 HTTPS HTTPS HTTP 同一台服务器上?

  1. Should I use jsonp when we request from http to https or https to http 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

你的情况的应用层协议是不同的,这就是为什么你的脚本失败

可能的解决方案是:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆