javascript – 网络服务在chrome中运行良好,但在Firefox和IE中没有
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 网络服务在chrome中运行良好,但在Firefox和IE中没有,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3500字,纯文字阅读大概需要6分钟。
内容图文
![javascript – 网络服务在chrome中运行良好,但在Firefox和IE中没有](/upload/InfoBanner/zyjiaocheng/756/c7cedacfeb5f4377895e80f5ff169081.jpg)
我试图调用外部Web服务,它在chrome中工作正常,但在firefox和IE中却没有.在chrome中,它返回’true’,但在firefox中,它返回’0 error’,这是我的完整代码…
<script type="text/javascript" src="js/jquery-1.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnCall").click(function (event) {
var campaignid = 1000007;
var r_source_id = 1000008;
var parameters = "{'CompanyName': '" + document.getElementById('txtCompanyName').value + "', 'name': '" + document.getElementById('txtName').value + "', 'title': '', 'email':'" + document.getElementById('txtEmail').value + "', 'phone':'" + document.getElementById('txtPhoneNo').value + "', 'web_url':'', 'no_of_emp':'0', 'c_Currency_id':'100', 'r_source_id':'" + r_source_id.toString() + "', 'industry_ID':'1', 'city':'', 'country_ID':'" + document.getElementById('ddlCountry').value + "', 'cur_solution':'','pur_timeline':'','comments':'', 'year_sell_erp':'2013', 'support':'', 'bpgroup_ID':'1', 'C_Campaign_ID':'" + campaignid.toString() + "', 'R_STATUS_ID':'1000033', 'C_Region_ID':'100', 'CreatedBy':'1000012', 'salesrep_id':'1000012', 'ad_org_id':'1000001', 'ad_client_id':'1000001', 'UpdatedBy':'100', 'AccessKey':'caff4eb4fbd6273e37e8a325e19f0991'}";
$.ajax({
type: "POST",
url: "http://cloudservice.softwareonthecloud.com/service.asmx/SetLead",
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (msg) {
AjaxSucceeded(msg);
},
error: AjaxFailed
});
});
});
function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
</script>
这是我上传此功能URL for testing
解决方法:
您收到此错误,因为您尝试在另一个域上调用Web服务.这违反了Same origin policy.这是一个安全限制.大多数旧版浏览器都会拒绝此类请求.
如果要在javascript中访问webservice的其他域,则需要设置Cross-Origin Resource Sharing.
Cross-origin resource sharing (CORS) is a mechanism that allows a web
page to make XMLHttpRequests to another domain. Such “cross-domain”
requests would otherwise be forbidden by web browsers, per the same
origin security policy. CORS defines a way in which the browser and
the server can interact to determine whether or not to allow the
cross-origin request
如果您可以访问Web服务代码,则可以在服务器上启用CORS请求.
Enable cors是一个很好的资源.这是一些explaination on cors
在IIS 7上,您需要在web.config中设置一些自定义标头.
<system.webserver>
<httpprotocol>
<customheaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customheaders>
</httpprotocol>
</system.webserver>
安全注意:这里有一个例子,我已经允许所有对服务器的请求.如果您要提供敏感数据,则可能需要将此限制为选定的域.
基于WCF的请求也可以通过WebOperationContext.Current.IncomingRequest进行检查,并且可以发送相应的标头.
旧版浏览器不支持CORS请求.你可以看到完整的browser compatibility list here
如果您必须支持旧版浏览器,或者无法更改Web服务,则可以始终在服务器上托管Web服务代理.您将请求发送到您的服务器,您的服务器请求原始webserivce中的数据.这样可以正常工作,因为它不违反交叉原始政策.一个简单的http处理程序可以作为服务器上的代理服务器.
以下是REST Web服务的示例http处理程序代理:
public void ProcessRequest(HttpContext context) {
WebClient myClient = new WebClient();
//Fetch response on your server
string response = myClient.DownloadString("http://example.com/webservice/webMethod");
// Send response to your javascript.
context.Response.Write(response);
}
然后,您可以根据需要从javascript调用此处理程序.
内容总结
以上是互联网集市为您收集整理的javascript – 网络服务在chrome中运行良好,但在Firefox和IE中没有全部内容,希望文章能够帮你解决javascript – 网络服务在chrome中运行良好,但在Firefox和IE中没有所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。