1

我有一个返回以下 JSON 的 .NET Web 服务:

{"command":"getqcardjson","valuesets":[{"datatable":"eaa65990-8f88-45e0-966b-610858ad4cb6","values":[{"Id":"eaa65990-8f88-45e0-966b-610858ad4cb6","InternalName":"Alpha Profile","TableName":"Alpha Profile","OnMainMenu":"","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"100","IsChained":"","IncludeInGlobalSearch":"False","IsModelObject":"","IsSchemaObject":"","IsSecured":"False","IsFileNewMenu":"False","IsUpdateShouldRippleCache":"False","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"Information.png","DefaultObjectLayoutStyle":"980163c2-2688-465d-9383-e5ad4dbf631f","Notes":"This is a test Enquiry profile object called Alpha Profile","Caption":"Alpha Profile","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"","IsGeocodable":""},{"Id":"2cbc4bc4-7db0-4344-b49a-bbc8ed4e9e60","InternalName":"CompMP","TableName":"CompMP","OnMainMenu":"True","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"100","IsChained":"","IncludeInGlobalSearch":"False","IsModelObject":"False","IsSchemaObject":"False","IsSecured":"","IsFileNewMenu":"False","IsUpdateShouldRippleCache":"","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"profile.png","DefaultObjectLayoutStyle":"980163c2-2688-465d-9383-e5ad4dbf631f","Notes":"","Caption":"CompMP","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"","IsGeocodable":""},{"Id":"f33038ce-2a64-4e79-99fc-ad6aaf90c2bb","InternalName":"ContactProfile","TableName":"ContactProfile","OnMainMenu":"True","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"100","IsChained":"","IncludeInGlobalSearch":"False","IsModelObject":"False","IsSchemaObject":"False","IsSecured":"","IsFileNewMenu":"False","IsUpdateShouldRippleCache":"","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"profile.png","DefaultObjectLayoutStyle":"980163c2-2688-465d-9383-e5ad4dbf631f","Notes":"","Caption":"ContactProfile","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"","IsGeocodable":""},{"Id":"fbb39ed3-0409-4712-b6dd-f6853b079aa6","InternalName":"QCardInqMP","TableName":"QCardInqMP","OnMainMenu":"True","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"100","IsChained":"","IncludeInGlobalSearch":"False","IsModelObject":"False","IsSchemaObject":"False","IsSecured":"","IsFileNewMenu":"False","IsUpdateShouldRippleCache":"","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"profile.gif","DefaultObjectLayoutStyle":"980163c2-2688-465d-9383-e5ad4dbf631f","Notes":"","Caption":"QCardInqMP","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"","IsGeocodable":""},{"Id":"41db39fb-fda8-4c5c-adc4-6e6c1ba77b90","InternalName":"QCEventAttendance","TableName":"QCEventAttendance","OnMainMenu":"True","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"","IsChained":"","IncludeInGlobalSearch":"","IsModelObject":"","IsSchemaObject":"","IsSecured":"","IsFileNewMenu":"","IsUpdateShouldRippleCache":"","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"","DefaultObjectLayoutStyle":"","Notes":"","Caption":"Attendance details","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"","IsGeocodable":""},{"Id":"8f186be5-d0ec-4dc7-bfd5-9b9190644c1f","InternalName":"QCFilms","TableName":"QCFilms","OnMainMenu":"True","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"","IsChained":"","IncludeInGlobalSearch":"False","IsModelObject":"","IsSchemaObject":"","IsSecured":"False","IsFileNewMenu":"False","IsUpdateShouldRippleCache":"False","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"","DefaultObjectLayoutStyle":"2b69073c-4035-4597-acc0-4d8345ba9d1c","Notes":"","Caption":"QCFilms","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"False","IsGeocodable":"False"}]}]}

我的客户端代码如下:

$('#panelMiddle').append('Getting Json');
    var url = "svc.asmx/GetJSONData?command=getqcardjson&qcardid=" + QCardId;
    $.ajax({
        url: url,
        dataType: 'json',
        success: function(data) {
            $('#panelMiddle').append('Success');
        },
        complete: function() {
            $('#panelMiddle').append('Completed');
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        $('#panelMiddle').append(' #ERROR:' + textStatus + '# ' + errorThrown + ' ');
        }

    });

如果我将返回的 JSON 粘贴到http://www.jsonlint.com/中,那么它会验证,但 jQuery AJAX 调用错误时会出现 textStatus="parsererror" 和 errorThrown="Invalid JSON"。我的 web svc 定义如下:

[WebMethod(EnableSession = true)] // Required to create web service, even when json decoration present
    [ScriptMethod(UseHttpGet = true,ResponseFormat = ResponseFormat.Json)] // Required decoration to ensure we can call using json
    public string GetJSONData(string command)
    { 

有谁知道发生了什么?谢谢。

Firefox 控制台响应显示如下:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://highpersoft.com/">{"command":"getqcardjson","valuesets":[{"datatable":"eaa65990-8f88-45e0-966b-610858ad4cb6","values":[{"Id":"eaa65990-8f88-45e0-966b-610858ad4cb6","InternalName":"Alpha Profile","TableName":"Alpha Profile","OnMainMenu":"","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"100","IsChained":"","IncludeInGlobalSearch":"False","IsModelObject":"","IsSchemaObject":"","IsSecured":"False","IsFileNewMenu":"False","IsUpdateShouldRippleCache":"False","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"Information.png","DefaultObjectLayoutStyle":"980163c2-2688-465d-9383-e5ad4dbf631f","Notes":"This is a test Enquiry profile object called Alpha Profile","Caption":"Alpha Profile","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"","IsGeocodable":""},{"Id":"2cbc4bc4-7db0-4344-b49a-bbc8ed4e9e60","InternalName":"CompMP","TableName":"CompMP","OnMainMenu":"True","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"100","IsChained":"","IncludeInGlobalSearch":"False","IsModelObject":"False","IsSchemaObject":"False","IsSecured":"","IsFileNewMenu":"False","IsUpdateShouldRippleCache":"","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"profile.png","DefaultObjectLayoutStyle":"980163c2-2688-465d-9383-e5ad4dbf631f","Notes":"","Caption":"CompMP","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"","IsGeocodable":""},{"Id":"f33038ce-2a64-4e79-99fc-ad6aaf90c2bb","InternalName":"ContactProfile","TableName":"ContactProfile","OnMainMenu":"True","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"100","IsChained":"","IncludeInGlobalSearch":"False","IsModelObject":"False","IsSchemaObject":"False","IsSecured":"","IsFileNewMenu":"False","IsUpdateShouldRippleCache":"","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"profile.png","DefaultObjectLayoutStyle":"980163c2-2688-465d-9383-e5ad4dbf631f","Notes":"","Caption":"ContactProfile","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"","IsGeocodable":""},{"Id":"fbb39ed3-0409-4712-b6dd-f6853b079aa6","InternalName":"QCardInqMP","TableName":"QCardInqMP","OnMainMenu":"True","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"100","IsChained":"","IncludeInGlobalSearch":"False","IsModelObject":"False","IsSchemaObject":"False","IsSecured":"","IsFileNewMenu":"False","IsUpdateShouldRippleCache":"","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"profile.gif","DefaultObjectLayoutStyle":"980163c2-2688-465d-9383-e5ad4dbf631f","Notes":"","Caption":"QCardInqMP","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"","IsGeocodable":""},{"Id":"41db39fb-fda8-4c5c-adc4-6e6c1ba77b90","InternalName":"QCEventAttendance","TableName":"QCEventAttendance","OnMainMenu":"True","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"","IsChained":"","IncludeInGlobalSearch":"","IsModelObject":"","IsSchemaObject":"","IsSecured":"","IsFileNewMenu":"","IsUpdateShouldRippleCache":"","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"","DefaultObjectLayoutStyle":"","Notes":"","Caption":"Attendance details","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"","IsGeocodable":""},{"Id":"8f186be5-d0ec-4dc7-bfd5-9b9190644c1f","InternalName":"QCFilms","TableName":"QCFilms","OnMainMenu":"True","DefaultDataSourceId":"bb4d1622-2e48-40b6-bea7-6355d701445c","DefaultOrder":"","IsChained":"","IncludeInGlobalSearch":"False","IsModelObject":"","IsSchemaObject":"","IsSecured":"False","IsFileNewMenu":"False","IsUpdateShouldRippleCache":"False","AdvancedSearchQueryId":"","CompoundViewQueryId":"","GlobalSearchQueryId":"","Icon":"","DefaultObjectLayoutStyle":"2b69073c-4035-4597-acc0-4d8345ba9d1c","Notes":"","Caption":"QCFilms","IsProfileObject":"True","ShortCacheQueryId":"","WorkflowForCreation":"","IsAllowNewFromExisting":"False","IsGeocodable":"False"}]}]}</string>

所以大概这就是问题所在。我上面的 web 方法声明有问题吗?

服务器现在这样做: {"Message":"Invalid JSON primitive: getqcardjson.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization .JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer 序列化程序)\r\n 在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize (JavaScriptSerializer 序列化程序,字符串输入,类型类型,Int32 depthLimit)\r\n 在 System.Web.Script.Services.RestHandler.GetRawParamsFromGetRequest(HttpContext 上下文,JavaScriptSerializer 序列化程序,WebServiceMethodData methodData)\r\n 在 System.Web.Script。服务.RestHandler。GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

4

1 回答 1

2

通常,您必须在contentType向 ASP.Net Web 服务发出 JSON 请求时设置,如下所示:

$.ajax({
  contentType: 'application/json; charset=utf-8',
  dataType: 'json',
  //rest of your current options..
});

否则,当 JSON 响应返回时,它会被包装在 XML 中。

于 2010-10-29T17:17:25.203 回答