无法分析JSON响应。JQUERY
当我试图从Web服务获取JSON时,我得到(status==“parserrror”),这意味着“无法解析JSON响应”。我做错了什么 MyService.asmx:无法分析JSON响应。JQUERY,jquery,asp.net,wcf,json,web-services,Jquery,Asp.net,Wcf,Json,Web Services,当我试图从Web服务获取JSON时,我得到(status==“parserrror”),这意味着“无法解析JSON响应”。我做错了什么 MyService.asmx: [WebMethod] public AssignmentInfo[] GetAssignmentInfo(int insId) { Proxies.ServiceRef.ServiceClient c = new Proxies.ServiceRef.ServiceCli
[WebMethod]
public AssignmentInfo[] GetAssignmentInfo(int insId)
{
Proxies.ServiceRef.ServiceClient c = new Proxies.ServiceRef.ServiceClient();
return c.GetAssignmentInfo(Id).ToArray();
}
.aspx页面中的jquery实现
this.ServiceProxy = function (serviceUrl) {
var _I = this;
this.serviceUrl = serviceUrl;
this.isWcf = false;
this.timeout = 10000;
this.contentType = "application/json";
this.invoke = function (method, params, callback, errorHandler) {
var jsonData = _I.isWcf ? JSON.stringifyWcf(params) : JSON.stringify(params);
// Service endpoint URL
var url = _I.serviceUrl + method;
$.ajax({
url: url,
data: jsonData,
type: "POST",
contentType: _I.contentType,
timeout: _I.timeout,
dataType: "serviceproxy", // custom type to avoid double parse
dataFilter: function (jsonString, type) {
if (type == "serviceproxy") {
// Use json library so we can fix up dates
var res = JSON.parseWithDate(jsonString);
if (res && res.hasOwnProperty("d"))
res = res.d;
return res;
}
return jsonString;
},
success: function (result) {
if (callback)
callback(result);
},
error: function (xhr, status) {
var err = null;
if (xhr.readyState == 4) {
var res = xhr.responseText;
if (res && res.charAt(0) == '{' && status != "parsererror")
var err = JSON.parse(res);
if (!err) {
if (xhr.status && xhr.status != 200)
err = new CallbackException(xhr.status + " " + xhr.statusText);
else {
if (status == "parsererror")
status = "Unable to parse JSON response.";
else if (status == "timeout")
status = "Request timed out.";
else if (status == "error")
status = "Unknown error";
err = new CallbackException("Callback Error: " + status);
}
err.detail = res;
}
}
if (!err)
err = new CallbackException("Callback Error: " + status);
if (errorHandler)
errorHandler(err, _I, xhr);
}
});
}
}
var serviceUrl = "service/MyService.asmx/";
var proxy = new ServiceProxy(serviceUrl);
function showAssignInfo() {
proxy.invoke("GetAssignmentInfo",
{ insId: $("#IAssignmentId").val() },
function (result) {
$.each(result, function (index) {
alert (this.ClaimId);
});
}, onPageError);
更新1:
JSON响应:
{"d":[{"__type":"Proxies.AFARServiceRef.AssignmentInfo","ExtensionData":{},"AssignDate":"\/Date(1317748587667)\/","AssignFileName":null,"ClaimId":"PA026195","ClaimantName":"Rachel Weiss","FirstContactDate":"\/Date(1302678000000)\/","FirstContactTime":{"Ticks":433800000000,"Days":0,"Hours":12,"Milliseconds":0,"Minutes":3,"Seconds":0,"TotalDays":0.50208333333333333,"TotalHours":12.049999999999999,"TotalMilliseconds":43380000,"TotalMinutes":723,"TotalSeconds":43380},"Id":5257,"InspectionDate":"\/Date(1302246000000)\/","StatusId":1,"SubmittedCount":5,"UploadedCount":9}]}
试一试
你能发布json响应吗?我猜你的意思是GetAssignmentInfo的返回是字符串。如果是这样,当我调试时,我会在strJSON中得到一个字符串“System.Collections.Generic.List`1[Proxies.ServiceRef.AssignmentInfo]”,而不是实际的数据。当你不使用javascriptSerializer时,你能发布你(在firebug中)得到的json响应吗?这将给出一个更清晰的画面。我猜json格式不太好我很抱歉我从未使用过firefox。我安装了firebug如何/在何处可以在firebug中看到json结果。没问题。。。点击f12将出现一个窗口,您将看到一些选项卡,进入
Net
选项卡,在其子选项卡中,您将看到XHR
,选择它,然后现在刷新将访问webservice
的引发事件页面。您将在XHR
选项卡中看到一些活动,请求正在发出,在xhr中,您将注意到Response
,其中将包含WebService在编辑后发送的响应。我看到断点击中了Webmethod,但从那里控件突然返回浏览器(不返回javascript函数)。
[WebMethod]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
public AssignmentInfo[] GetAssignmentInfo(int insId)
{
Proxies.ServiceRef.ServiceClient c = new Proxies.ServiceRef.ServiceClient();
return c.GetAssignmentInfo(Id).ToArray();
}