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
I make the assuption based on comments that you can directly go to the web service in the browser.
Just to isolate your custom object from configuration, you could put another service in place like:
[WebMethod]
public static string GetServerTimeString()
{
return "Current Server Time: " + DateTime.Now.ToString();
}
Call that from a client side jQuery ajax call. If this works, then it is probably related to your object specifically and not configuration on the server side. Otherwise, keep looking on the server side config track.
EDIT: Some sample code:
[WebMethod(EnableSession = true)]
public Category[] GetCategoryList()
{
return GetCategories();
}
private Category[] GetCategories()
{
List category = new List();
CategoryCollection matchingCategories = CategoryList.GetCategoryList();
foreach (Category CategoryRow in matchingCategories)
{
category.Add(new Category(CategoryRow.CategoryId, CategoryRow.CategoryName));
}
return category.ToArray();
}
And here is an example of where I post a complex data type JSON value
[WebMethod]
public static string SaveProcedureList(NewProcedureData procedureSaveData)
{
...do stuff here with my object
}
This actually includes two arrays of objects inside it... my NewProcedureData type is defined in a class which lays those out.
EDIT2:
Here is how I handle a complex object in one instance:
function cptRow(cptCode, cptCodeText, rowIndex)
{
this.cptCode = cptCode;
this.cptCodeText = cptCodeText;
this.modifierList = new Array();
//...more stuff here you get the idea
}
/* set up the save object */
function procedureSet()
{
this.provider = $('select#providerSelect option:selected').val(); // currentPageDoctor;
this.patientIdtdb = currentPatientIdtdb;// a javascript object (string)
//...more object build stuff.
this.cptRows = Array();
for (i = 0; i < currentRowCount; i++)
{
if ($('.cptIcdLinkRow').eq(i).find('.cptEntryArea').val() != watermarkText)
{
this.cptRows[i] = new cptRow($('.cptIcdLinkRow').eq(i).find('.cptCode').val(), $('.cptIcdLinkRow').eq(i).find('.cptEntryArea').val(), i);//this is a javscript function that handles the array object
};
};
};
//here is and example where I wrap up the object
function SaveCurrentProcedures()
{
var currentSet = new procedureSet();
var procedureData = "";
var testData = { procedureSaveData: currentSet };
procedureData = JSON.stringify(testData);
SaveProceduresData(procedureData);
};
function SaveProceduresData(procedureSaveData)
{
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: procedureSaveData,
the rest of the ajax call...
});
};
NOTE !IMPORTANT the procedureSaveData name must match exactly on the client and server side for this to work properly. EDIT3: more code example:
using System;
using System.Collections.Generic;
using System.Web;
namespace MyNamespace.NewProcedure.BL
{
///
/// lists of objects, names must match the JavaScript names
///
public class NewProcedureData
{
private string _patientId = "";
private string _patientIdTdb = "";
private List _cptRows = new List();
public NewProcedureData()
{
}
public string PatientIdTdb
{
get { return _patientIdTdb; }
set { _patientIdTdb = value; }
}
public string PatientId
{
get { return _patientId; }
set { _patientId = value; }
}
public List CptRows = new List();
}
--------
using System;
using System.Collections.Generic;
using System.Web;
namespace MyNamespace.NewProcedure.BL
{
///
/// lists of objects, names must match the JavaScript names
///
public class CptRows
{
private string _cptCode = "";
private string _cptCodeText = "";
public CptRows()
{
}
public string CptCode
{
get { return _cptCode; }
set { _cptCode = value; }
}
public string CptCodeText
{
get { return _cptCodeText; }
set { _cptCodeText = value; }
}
}
}
Hope this helps.