jQuery Ajax Cross-site scripting -
i know question has been asked (many times) before still can't seem right.
i want perform ajax request in jquery , "arbitrary" contents - e.g. may html, text, json, img
when perform following infamous no 'access-control-allow-origin' header present on requested resource. origin 'http://mydomain.com' therefore not allowed access.
$.ajax({ url: "http://www.pureexample.com/jquery/cross-domain-ajax.html", // datatype: "jsonp", crossdomain: true, success: function (data) { console.log('success'); console.log(data); }, error: function(request, status, error) { console.log('error on request. ' + request.responsetext); alert('error on request. ' + request.responsetext); } });
i not serving data, cannot on server-side allow cross domain request. believe specifying jsonp (commented out) assumes json being returned? or without it, not work.
so, bottom line - there "simple" way to cross-domain request may result , insert tag (e.g. html div)
any help, or further explanation appreciated.
thanks. rob
i had problem while ago, when using external webservice retrieve brazilian's zip code data (address , such). and used same method did.
there few points need stressed out:
1) ajax not allow security protocol differences. means that, if page secured behind secure protocol (https), browser not allow external server return through unsecured 1 (http). (i found late in development, , result use .net's webrequest )
2) setting datatype "jsonp" doesn't transform response json. allows received such.
3) jsonp only datatype acceptd cross-domain requests. error message described due reason.
so, summarize:
when talking cross-domain requests ajax,"arbitrary" contents restricted json(p). , data received must json.
also, until jquery 1.5, if response not correctly typed json, no errors raised .
i wish had known these issues before started development... uploading component production servers unbeknownst of fact requesting data stored within http ssl-layered servers quite embarassment. ... (though still quite predictable, actually)
for more info : http://api.jquery.com/jquery.ajax/
Comments
Post a Comment