简体   繁体   中英

asmx round trip json

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\"}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM