1

I am calling an asmx webservice which returns json (msg.d) that is consumed properly by knockout.js. When I attempt to return the identical json to asmx, I get error messages. Is there something obvious I'm missing? ... msg.d is a well formed object array.

calling storeGroupCategories(msg.d); returns webservice error ...

{"Message":"Invalid JSON primitive: Baby+Books."

calling storeGroupCategories(msg); returns webservice error ...

{"Message":"Invalid JSON primitive: d."

WebService

public class kbo_inexcludecategories : WebService
{

    [WebMethod]
    public List<Group> GetIncludeExcludeJson()
    {
        var Groups =  new List<Group>();
        ShopAssistGroupHandler.getInExCategories(Groups);
        return Groups;
    }

    [WebMethod]
    public GroupGuid StoreGroupCategories(List<InExCategory> inExCategories)
    {
        var inExString = JsonConvert.SerializeObject(inExCategories);
        var returnGuid = DataHandler.SaveGroupJsonString(inExString);
        return new GroupGuid(returnGuid);
    }
}

Associated json ...

var _url = "kbo-inexcludecategories.asmx/";
var _method = "GetIncludeExcludeJson";
var _jsonData = "{}";

function storeGroupCategories(groupCategories) {
    if(groupCategories != ""){
        showProgressBar("Storing Group Categories");
        getJsonData(_url, "StoreGroupCategories", groupCategories);
    }
}

function getGroupMatrix() {
    showProgressBar("Loading Group Categories");
    getJsonData(_url, _method, _jsonData);
}

function getJsonData(url, method, jsonData) {
    var myUrl = url + method;
    $.ajax({
        type: "POST",
        url: myUrl,
        data: jsonData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: false, //blocks window close
        success: onSuccess,
        error: onError
    });
}

function onSuccess(msg) {

    // Hide the fake progress indicator graphic.
    hideProgressBar("");
    if(msg.d.hasOwnProperty("Guid")) {
        saveGroupGuid(msg.d);
    }
    else {
        storeGroupCategories(msg.d);
        //showGroupAccordion(msg.d);
        //OpenAdvancedDialog();
    }
}

json sample ...

"{\"groups\":[{\"__type\":\"group\",\"id\":1488,\"name\":\"Baby Books\",\"categories\":
[{\"__type\":\"groupcategory\",\"id\":152,\"name\":\"Activity Books\",\"value\":\"Included\"},
{\"__type\":\"groupcategory\",\"id\":167,\"name\":\"Bedtime and Dreams\",\"value\":\"Excluded\"}
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
BedfordNYGuy
  • 383
  • 2
  • 9
  • POssibly, you need to set teh response type to JSON http://stackoverflow.com/questions/211348/how-to-let-an-asmx-file-output-json – MattC Feb 11 '14 at 10:03

1 Answers1

0

To begin with I think you need to pass your json in like this:

storeGroupCategories(msg.d)

But within this function you also need to create valid json parameters to the post, which would look like this:

getJsonData(_url, "StoreGroupCategories", "{ inExCategories: " + groupCategories + " }");

I would also change your signature to the following, so groups matches the argument you are passing across:

public GroupGuid StoreGroupCategories(List<InExCategory> groups)

If you put a break point in the web page method, you will see exactly what is coming across, and check that it is what you are expecting.

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
  • Still got error, when I added JSON.stringify to object in call getJsonData(_url, "StoreGroupCategories", "{ inExCategories: " + JSON.stringify(groupCategories) + " }"); I got a simpler error .... ":"Invalid web service call, missing value for parameter: \u0027groups\u0027."," – BedfordNYGuy Feb 11 '14 at 15:17
  • You shouldn't need to stringify, just try it without. – hutchonoid Feb 11 '14 at 15:18
  • I did that first & got ... {"Message":"Invalid JSON primitive: object – BedfordNYGuy Feb 11 '14 at 15:25
  • Ok, there must be some kind of json serialize issue before you send it across. Can you debug and check that it resembles something like: data: '{"groups":[{"__type\":"group","id":1488,"name":"Baby Books","categories": [{"__type":"groupcategory","id" ...' – hutchonoid Feb 11 '14 at 15:39
  • Thanks for getting me on the right track. I had to make name changes: public GroupGuid StoreGroupCategories(List groups) { var inExString = JsonConvert.SerializeObject(groups); and "{ groups: " + groupCategories + " }" where groupCategories was stringified. All is well with the world. – BedfordNYGuy Feb 11 '14 at 18:41