本篇文章主要介绍了"Ajax 在IE 9下跨域访问",主要涉及到JSONP方面的内容,对于Javascriptjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:
IE 9- XDomainRequest Domain Crossing Ajax Request.Backgroud:I had a problem on A...
IE 9- XDomainRequest Domain Crossing Ajax Request.
Backgroud:
I had a problem on Ajax call for some data from an azure service in one of my projects. it usually happens in the scenario like this: There is a page under one domain system. However, we need display the data from another domain system on this page. So it brings a problem of cross-domain. e.g. $.getJSON('another domain url', success callback function);
Issue
Generally, It can be solved by jQuery Aajx. However, JQuery JSONP may not work properly under IE 9- browser. That's the problem i need face with.
Solution
I research on many blogs and articles. There are two way to solve this problem: 1. Iframe: you can create an invisible iframe element and try to load the data as a text of iframe. Then you can get it by JS. however, if you have no right to get the support from background, it may not a good idea. 2. XDomainRequest: Microsoft provide a plugin called 'XDomainRequest' to support the cross-domain call under IE 9-. This is the solution i want to introduce.
This is a sample of the usage of XDomainRequest: var requester = new XDomainRequest(); requester.open('get', 'another domain request url'); requester.contentType = 'text/plain'; requester.onload = function (event) { //get data from requester.responseText console.log('success'); }; requester.onerror = function () { console.log('error'); }; requester.send();
However, it may not work in your code. Because you may put this piece of code in one of page events. that's a problem. because there is a limitation of the usage of XDomainRequest. IE 9- asks you to initialize the XDomainRequest under a global scope instead of one event handle. For example, The code is wrong like this:
$(function(){ var requester = new XDomainRequest(); requester.open('get', 'another domain request url'); requester.contentType = 'text/plain'; requester.onload = function (event) { //get data from requester.responseText console.log('success'); }; requester.onerror = function () { console.log('error'); }; requester.send(); });
The correct way should be like this: var requester = new XDomainRequest();
$(function(){ requester.open('get', 'another domain request url'); requester.contentType = 'text/plain'; requester.onload = function (event) { //get data from requester.responseText console.log('success'); }; requester.onerror = function () { console.log('error'); }; requester.send(); });
This is the point i want to tell you here. Thanks.
以上就介绍了Ajax 在IE 9下跨域访问,包括了JSONP方面的内容,希望对Javascriptjrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_201965.html