views:

31

answers:

1

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","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/"&gt;{"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"}

+1  A: 

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.

Nick Craver
Great - that has stopped the error being thrown. However, none of the ajax callback functions are now running (i.e. the success function isn't running, nor is the complete function.) So it has fixed the problem, but I'm left with no response at all...
DEH
@DEH -you sure you're not getting a JavaScript error in the console?
Nick Craver
@Nick - I am indeed getting that - I have added the StackTrace to the end of the question...
DEH
@DEH - You should format your parameter as JSON as well, so this: `var url = "svc.asmx/GetJSONData?command=getqcardjson` should be: `var url = "svc.asmx/GetJSONData";` then you should have `data: '{"command":"getqcardjson"}'` option in `$.ajax()`.
Nick Craver
Ok, sorted. I changed the url to var url = "hpssvc.asmx/GetJSONData?command=\"getqcardjson\"i.e. I enclosed the command in double-quotes. So, the contentType was the answer, but then I needed to tweak the url to support the new contentType. Thanks very much everyone!
DEH
@Nick - great - that's a better way of doing it. Thanks very much again.
DEH
@DEH - welcome :)
Nick Craver