大佬教程收集整理的这篇文章主要介绍了javascript – 从ie / firefox / chrome中的jquery调用跨域.net方法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
function save() { if (jQuery.browser.msie && window.XDomainrequest) { // Use XDR var params = "{'height':" + 10 + ",'width':" + 10 + ",'pos':'" + 10 + "'}"; var xdr = new XDomainrequest(); xdr.onerror = alert_error; xdr.ontimeout = alert_timeout; xdr.onprogress = alert_progress; xdr.onload = alert_loaded; xdr.timeout = 10000; xdr.open("post",'http://domain/reciever.asmx/setdata'); //Tried as webservice and as a normal aspx page //xdr.open("post",'http://domain/default.aspx'); xdr.send(params); } else { var params = "pos=" + positions + "&width=" + screenWidth + "&height=" + screenHeight; var myAjax = new jQuery.ajax( "http://domain/default.aspx",{ type: 'post',cache: false,crossDomain: true,data: params }); } }
在服务器端,web.config具有:
<httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol>
和webservice
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public String setdata(int width,int height,String pos)
aspx@L_772_8@返回:
Response.Clear(); Response.ContentType = "text/plain"; response.addheader("Access-Control-Allow-Origin","*"); Response.End();
提琴手说:
fiddler在会话#2565中检测到协议违规.
内容长度不匹配:请求标头指示38个字节,但客户端发送0个字节.所以我相信它是“Access-Control-Allow-Origin”,但我已经设置了(据我所知).
有人可以帮我理解我做错了什么.
@H_301_4@
但解决方案是使用JSONP调用而不是ajax调用. JSONP通过发出适合脚本文件的请求来避免这种情况.通过使用JSONP,以下事情可以使跨域请求成为可能,
1.浏览器首先创建一个新的脚本标记以注入HTML DOM,而不是访问XHR对象.
2.脚本标记的URL设置为您希望获取/发布(使用http GET)数据的URl.
4.请求被发送到服务器,即使它是跨域的
请参阅下面的网址以获取实施细节,
http://www.codeproject.com/Articles/78757/Making-Cross-Domain-jQuery-AJAX-Calls.aspx
http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide
希望这绝对可以帮助你……
@H_301_4@ @H_301_4@以上是大佬教程为你收集整理的javascript – 从ie / firefox / chrome中的jquery调用跨域.net方法全部内容,希望文章能够帮你解决javascript – 从ie / firefox / chrome中的jquery调用跨域.net方法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。