如何使用jQuery进行跨站点调用?

我有一个现有的jQuery插件,可以进行很多AJAX调用(主要是JSON)。我想知道允许它进行跨站点调用的最快方法是什么?即$ .get和$ .post URL不会来自同一域。 我听说可以用JSONP,但我想知道是否有人可以给我一个具体的例子来介绍整个过程? 如果可能,我想对脚本进行最小的更改。或者,我也可以试试proxy.php?

If you have control over the remote domain or the remote domain has a permissive crossdomain.xml you can drop in a library like flXHR in conjunction with its jQuery plugin.

JSONP will allow you to do cross-site calls. See jQuery docs on that matter.

The concept is simple: instead of doing a normal Ajax call, jQuery will append a <script> tag to your <head>. In order for this to work, your JSON data needs to be wrapped in a function call.

Your server needs to send information in such way (PHP example):

$json = json_encode($data);
echo $_GET['jsonp_callback'] . '(' . $json . ');';

Then, you can use jQuery to fetch that information:

$.ajax({
  dataType: 'jsonp',
  jsonp: 'jsonp_callback',
  url: 'http://myotherserver.com/getdata',
  success: function () {
    // do stuff
  },
});

More information is available here: What is JSONP?

You can also use CORS instead of JSONP, works with ff,chrome,safari. CORS is less troublesome to setup and requires only a filter in server-side.

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.

http://techblog.constantcontact.com/software-development/using-cors-for-cross-domain-ajax-requests/