我正在尝试调用外部网络服务,它在 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 .这是一个安全限制。大多数旧浏览器会拒绝此类请求。
您需要设置 Cross-Origin Resource Sharing如果您想访问其他域,请使用 javascript 中的网络服务。
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
如果您有权访问网络服务代码,则可以在服务器上启用 CORS 请求。
Enable cors是一个很好的资源。这是一些explaination on cors
在 IIS 7 上,您需要在 web.config 中设置一些自定义 header 。
<system.webserver>
<httpprotocol>
<customheaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customheaders>
</httpprotocol>
</system.webserver>
安全说明:举个例子,我已经允许对服务器的所有请求。如果您提供敏感数据,您可能希望将此限制为选定的域。
也可以通过 WebOperationContext.Current.IncomingRequest
检查基于 WCF 的请求,并且可以发送适当的 header 。
旧版浏览器不支持 CORS 请求。你可以看到完整的 browser compatibility list here
如果您必须支持旧版浏览器,或者无法更改网络服务,您始终可以在您的服务器上托管网络服务代理。您将请求发送到您的服务器,您的服务器从原始 webserivce 请求数据。这很好用,因为它不违反跨源策略。一个简单的 http 处理程序可以作为您服务器上的代理服务器。
REST 网络服务的示例 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 - Web 服务在 chrome 中工作正常,但在 firefox 和 IE 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14241324/