[英]Cross-site AJAX using jQuery
I have an existing jQuery plugin which makes a lot of AJAX calls (mostly JSON). 我有一个现有的jQuery插件,它可以进行大量的AJAX调用(主要是JSON)。 I am wondering what is the quickest to allow it to do cross-site calls ie the $.get and $.post URL's will not be from the same domain.
我想知道什么是最快允许它进行跨站点调用,即$ .get和$ .post URL不会来自同一个域。
I have heard of JSONP, but was wondering if someone could give me an concrete example to go about the whole process. 我听说过JSONP,但是想知道是否有人可以给我一个具体的例子来讲述整个过程。 I want to make minimal changes if possible to my script.
我希望尽可能对我的脚本进行微小的更改。 Should I use a proxy.php of sorts?
我应该使用各种proxy.php吗?
Thank you for your time. 感谢您的时间。
JSONP will allow you to do cross-site calls. JSONP将允许您进行跨站点呼叫。 See jQuery docs on that matter.
请参阅jQuery文档。
The concept is simple: instead of doing a normal Ajax call, jQuery will append a <script>
tag to your <head>
. 这个概念很简单:jQuery不会执行普通的Ajax调用,而是将
<script>
标记附加到<head>
。 In order for this to work, your JSON data needs to be wrapped in a function call. 为了使其工作,您的JSON数据需要包含在函数调用中。
Your server needs to send information in such way (PHP example): 您的服务器需要以这种方式发送信息(PHP示例):
$json = json_encode($data);
echo $_GET['jsonp_callback'] . '(' . $json . ');';
Then, you can use jQuery to fetch that information: 然后,您可以使用jQuery来获取该信息:
$.ajax({
dataType: 'jsonp',
jsonp: 'jsonp_callback',
url: 'http://myotherserver.com/getdata',
success: function () {
// do stuff
},
});
More information is available here: What is JSONP? 有关更多信息,请参见: JSONP是什么?
如果您可以控制远程域,或者远程域具有许可的crossdomain.xml ,则可以将其与其jQuery插件一起放入像flXHR这样的库中。
You can also use CORS instead of JSONP, works with ff,chrome,safari. 您也可以使用CORS而不是JSONP,与ff,chrome,safari一起使用。 CORS is less troublesome to setup and requires only a filter in server-side.
CORS设置起来不那么麻烦,只需要服务器端的过滤器。
Please go through this article.Well explained and similar. 请仔细阅读这篇文章。说明并且类似。 Only constraint is IE does not support this and older versions of FF,chrome also has some issues.
只有约束是IE不支持这个和旧版本的FF,chrome也有一些问题。
http://techblog.constantcontact.com/software-development/using-cors-for-cross-domain-ajax-requests/ http://techblog.constantcontact.com/software-development/using-cors-for-cross-domain-ajax-requests/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.