我有点想不通,情况如下:
我在不在同一台机器上的 Web 服务上调用一个方法,并在我的脚本中使用以下 JS 片段:
$.ajax({
type: "POST",
url: "http://" + gServer + "/xawebservice/xawebservice.asmx/" + webMethod,
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
async: false,
data: WSParameters,
success: function callFunction(result) { processResults(result, pType, pParam1); },
error: function (xhr, status, error) {
alert(error.toString());
//alert(xhr.toString());
}
});
参数很好,经过测试,web方法也正确。
作为错误消息,我得到了这个:
- Firefox:[异常...“失败”nsresult:“0x80004005(NS_ERROR_FAILURE)”位置:“JS 框架 :: http://
localhost
:7515/jquery-1.8.3.js :: :: line 8434”数据:否] - Chrome:错误:NETWORK_ERR:XMLHttpRequest 异常 101
- IE8:无传输
如果我在同一台机器上运行的 Web 服务上使用相同的代码段,则没有问题。如果我通过 Web 界面使用远程 Web 服务,它也可以正常工作。
PS:我google了一下,有些页面推荐了一些跨域参数,这也不起作用。不幸的是,我猜使用相对路径是行不通的。
感谢您提前做出的任何努力。
溴 vm370
更新:好吧,我更新了我的代码以根据我现有的请求执行 CORS 请求,但我收到错误 500,直接在服务器上执行请求工作正常,并且在服务器上激活了 CORS。
function xenappRequest(pType, pParam1) {
// CORS request
var url = "http://" + gServer + "/webservice/webservice.asmx/webMethod";
var params = { "appName": pParam1 };
var xhr = createCORSRequest("POST", url);
if (!xhr) {
alert('CORS not supported');
} else {
// Do the request
// Response handlers.
xhr.onload = function () {
//var text = xhr.responseText;
alert('Response from CORS request to ' + url + ': ' + xhr.response);
};
xhr.onerror = function () {
alert('Woops, there was an error making the request.');
};
xhr.send(JSON.stringify(params));
}
}
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
从FF我得到错误500,在IE8中请求落在xhr.onerror子句中......有什么想法吗?