我正在尝试使用 jquery 从客户端代码调用 asmx webservice。但是,我发现要真正实现这一点非常困难。任何人都可以帮忙吗?
怎么了:
JsonConvert 是我使用 NewtonSoft 制作的一个库,用于将对象转换为 json 以返回。
asmx 托管在域 b.company.com 上
客户端 ajax 调用托管在域 a.compay.com 上
Fiddler 接听单次呼叫 two(2) 401,然后接听 200,这在回复的 JSON 选项卡中不可见。
这是 asmx 代码:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string PingCRM()
{
try
{
return JsonConvert.SerializeObject(new
{
Status = "OK",
Data = "Hello CRM"
});
}
catch (Exception ex)
{
return JsonConvert.SerializeObject(new
{
Status = "ERROR",
Data = ex
});
}
}
这是一个ajax jquery代码:
$.ajax({
url: url,
type: "POST",
data: {},
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (result) {
console.log('ajax success');
console.log('result = [' + result + ']');
},
error: function (xhr, status, error) {
var txtError = 'status = [' + status + ']; Respsonse = [' + xhr.responseText + ']; Respsonse = [' + error + ']';
console.log(txtError);
}
});
这是响应的提琴手“原始”视图:
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Persistent-Auth: true
X-UA-Compatible: IE=9
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type, Accept
Access-Control-Allow-Methods: GET, POST
WWW-Authenticate: Negotiate oRswGaADCgEAoxIEEAEAAACWCH0UgarqcwAAAAA=
Date: Thu, 15 Aug 2013 22:47:12 GMT
Content-Length: 127
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://b.company.com/">{"Status":"OK","Data":"Hello CRM"}</string>