I want to create a web method that accepts a List of custom objects (passed in via jQuery/JSON).
When I run the website locally everything seems to work. jQuery an
Have to say, I am not 100% sold my answer is (all) the issue, BUT this would address the deserialization. Adding some detail for your specific object.
on client side:
function itemObject (objectTitle, objectDescription ,objectValue )
{
this.ObjectTitle = objectTitle,;
this.ObjectDescription = objectDescription ;
this.ObjectValue = objectValue ;
};
// 4 objects in source-fix for your list
function objRow()
{
this.myCode = myCode;
this.myCodeText = myCodeText;
this.customObjectList = new Array();
for (k = 0; k < 4; k++)
{
this.customObjectList [k] = new itemObject (sourceTitle[k],sourceDescription[k],sourceValue[k]);
};
};
var myCode = "hicode1";
var myCodeText = "hicodeText1";
$(function()
{
function SaveCurrentObjects()
{
var currentSet = new objRow();
var objectData = "";
var testData = {objectSaveData : currentSet };
objectData = JSON.stringify(testData);
SaveObjectsData(objectData );
};
/* save the objects */
function SaveObjectsData(objectSaveData)
{
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: objectSaveData,
dataFilter: function(data)
{
var msg;
if (typeof (JSON) !== 'undefined' &&
typeof (JSON.parse) === 'function')
msg = JSON.parse(data);
else
msg = eval('(' + data + ')');
if (msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
},
url: "/manageobjects.asmx/EditCustomObjects",
success: function(msg)
{
//do stuff
},
failure: function(msg)
{
//handlefail
}
});
};
});
on server side you would have:(exact name of "objectSaveData") as in client code.
[WebMethod]
public static string EditCustomObjects(ObjectData objectSaveData)
{
// code to save your object
return "saved objects";
}
create a ObjectData class cointaining a list of objectItem similar to my other example matching up the objects (careful of names).
NOTE: I have the ASP.NET ajax extensions installed - working with asp.net 2.0 code for this example.
Maybe the path you are passing with jquery does not exist try to see the response using FireBug.
This is a wrong declaration in javascript.
var itemObject = {
ObjectTitle = objectTitle,
ObjectDescription = objectDescription,
ObjectValue = objectValue
}
Correct object initialization should be:
var itemObject = {
ObjectTitle : objectTitle,
ObjectDescription : objectDescription,
ObjectValue : objectValue
};
Also try temporary removing async: false
from $.ajax
's parameters.
Alright, here's how it is for an object, just tested this: ============================================ASPX==================================
$("#btngeMethodCallWithAnObjectAsAParameter").click(function (event) {
$.ajax({
type: 'POST',
url: 'Default.aspx/PageMethodCallWithAnObjectAsAParameter',
data: '{"id":"' + '5' + '", "jSonAsset":' + JSON.stringify(new BuildJSonAsset()) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d) {
alert(msg.d);
}
},
error: function () {
alert("Error! Try again...");
}
});
});
});
function BuildJSonAsset() {
this.AssetId = '100';
this.ContentId = '200';
this.AssetName = 'Asset1';
}
========================================================================
[WebMethod()]
public static string PageMethodCallWithAnObjectAsAParameter(int id, JSonAsset jSonAsset)
{
return "Success!";
}
======================================================================
And this works for a single object, next I am going to try a list of objects:)