[英]JSON Parse Error Garble
我正在嘗試使用jquery從客戶端代碼調用asmx Web服務。 但是,我發現要真正實現這一目標非常困難。 有人可以幫忙嗎?
怎么了:
JsonConvert是我由NewtonSoft使用的庫,用於將對象轉換為json以返回。
asmx托管在域b.company.com上
客戶端ajax調用托管在域a.compay.com上
Fiddler會接聽單打二(2)401,然后接一個200,該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>
響應被壓縮(請參閱響應頭: Content-Encoding: gzip
)。 Fiddler應該能夠為您解壓縮,通常是通過單擊響應頂部的欄(請參閱下文)。 如果看不到,請嘗試升級到Fiddler的較新版本。
更新后在問題中進行編輯 :現在您遇到的問題是響應不是JSON格式,而是XML(XML文檔中包含JSON),因此$.ajax
將無法在result
參數。 您可以嘗試將服務器更改為實際返回JSON而不是XML,或者可以從jqXHR
對象獲取響應文本,該對象將作為第三個參數傳遞給success
函數,從XML中提取JSON並將其解析為結果自己。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.