I have a .NET web service returning the following JSON:
{"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"开发者_如何学JAVA,"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"}]}]}
My client code is as follows:
$('#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 + ' ');
}
});
If I paste the returned JSON into http://www.jsonlint.com/ then it validates, but the jQuery AJAX call errors with textStatus="parsererror" and errorThrown="Invalid JSON". My web svc is defined as follows:
[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)
{
Anyone know whats going on? Thanks.
Firefox console response shows as follows:
<?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>
so presumably that's the problem. Something wrong with my web method declarations above?
Server now doing this: {"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 at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromGetRequest(HttpContext context, JavaScriptSerializer serializer, WebServiceMethodData methodData)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
Typically you have to set the contentType
when making a JSON request to an ASP.Net web service, like this:
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
//rest of your current options..
});
Otherwise that JSON response is wrapped in XML when it comes back.
精彩评论