site AJAX using jQuery

I have an existing jQuery plugin which makes a lot of AJAX calls (mostly 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.

I have heard of JSONP, but was wondering if someone could give me an concrete example to go about the whole process. I want to make minimal changes if possible to my script. Should I use a proxy.php of sorts?

Thank you for your time.


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?


如果您可以控制远程域,或者远程域具有宽容的crossdomain.xml,则可以将其放入像flXHR这样的库并结合其jQuery插件。


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/

链接地址: http://www.djcxy.com/p/46242.html

上一篇: jquery $ .ajax自定义http头问题

下一篇: 使用jQuery的网站AJAX