Modify XMLHttpRequests to make them cross domain
I am working on a proxy for an application. I need to convert all the AJAX calls on a particular page to be cross domain so that my proxy can complete those ajax calls.
I was looking into possibility of binding JQuery.beforeSend with window and have three questions:
How can I actually add extra parameter of dataType: jsonp; to all the AJAX calls that are present on the page.
Will this bind the function with all the XMLHttpRequests or only the requests done via jQuery $.ajax , $.get, $.post, etc functions
If I add dataType: jsonp; will it only add this to calls done via jQuery or all the calls done via any library or originating from browser
Is there a better way to do it. Any other recommendations to over come this problem. Since I w开发者_Python百科ont be knowing the external HTML, changing the URL it calls via AJAX is more harder and hence routing it via my proxy domain is not a preferred way in my case.
Thanks
You could shadow the existing jQuery method...
var jQueryAjax = $.ajax; $.ajax = function(settings) { settings.type = 'jsonp'; jQueryAjax(settings); }
I think this should work.
ajax()
is the low level interface for AJAX requests in jQuery, but I'm not sure if they call it.getJSON()
seems to callget()
.Adding
jsonp
will only be useful if the service you are using supports JSONP.
- set the datatype to jsonp ( dataType: "jsonp")
- With jsonp you can only handle GET request, see 3.
- Only to the call where you added jsonp as datatype
精彩评论