0

I have a script which returns a data array in JSON as per below.

[
    {"ItemID":"10319","ItemCode":"ITEM-010318","Qty":"1","custRef":"12 - Mitsubishi Fighter FK61F"},
    {"ItemID":"10933","ItemCode":"ITEM-010932","Qty":"1","custRef":"12 - Mitsubishi Fighter FK61F"},{"ItemID":"10537","ItemCode":"ITEM-010536","Qty":"1","custRef":"12 - Mitsubishi Fighter FK61F"},
    {"ItemID":"14863","ItemCode":"ITEM-014862","Qty":"1","custRef":"12 - Mitsubishi Fighter FK61F"},
    {"ItemID":"14864","ItemCode":"ITEM-014863","Qty":"1","custRef":"12 - Mitsubishi Fighter FK61F"}
]

This data is stored in a variable called cartData

I then push the data to my WebMethod via AJAX, as follows

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "OrderFormServices.asmx/AddItemsToCart",
    data: JSON.stringify(cartData),
    dataType: "json"

});

My web method looks like this

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void AddItemsToCart(string [] itemID, string [] itemCode, string [] Qty, string [] custRef)

However when I try and POST the data, I get an error on the console which I can't make head or tail of! Can anyone shed some light on it?

Type 'System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089],
[System.Object, mscorlib, 
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'
is not supported for deserialization of an array.

1 Answer 1

1

The method expects an array for each field. So, either you change method to be something like:

public void AddItemsToCart(CartItem[] items)

Where CartItem is class with all these fields.

OR

Edit the js to pass as the web method expects like:

var data   = {
itemID : [],
itemCode:[],
Qty:[],
custRef:[]};
//here fill all array with each one of the items in the json array.
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "OrderFormServices.asmx/AddItemsToCart",
    data: JSON.stringify(data),
    dataType: "json"

});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I've edited as per your second suggestion and I'm now using cartData.push({ 'itemID': itemIdVal, 'itemCode': itemCodeVal, 'qty': qtyVal, 'custRef': custRefVal }); however this doesn't seem to be working - cartData.push is not a function?
no this wouldnt work, it will work like this: cartData.push({ 'itemID': [itemIdVal], 'itemCode': [itemCodeVal], 'qty': [qtyVal], 'custRef': [custRefVal] }); Also be sure the param names are the same Case as the web method, for exmaple: Qty.
I get cartData.push is not a function?
It should be since its an array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.