我一直有這個代碼的問題,我花了最後3個小時挖掘並試圖找到答案。由於我沒有成功,我將只張貼代碼,並要求辦理哪種參數我應該有我的web服務這個請求:ASP.NET:jQuery AJAX的'數據'參數問題
var args = [{ key: 'myId', value: 'myValue' }, { key: 'myOtherId', value: 'myOtherValue'}];
var dataToSend = { name: 'fooId', value: 'fooValue', args: args };
$.ajax({
type: 'POST',
url: 'fooURL',
data: dataToSend,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: OnSuccess,
error: OnError
});
現在,我必須能夠哪種簽名得到我的「dataToSend」?
我已經試過:
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(string name, object value, List<Args> args)
{
return "OK";
}
public class Args
{
public string key { get; set; }
public object value { get; set; }
}
和
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(string name, object value, object[] args)
{
return "OK";
}
也
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Foo(dataToSend dataToSend)
{
return "OK";
}
public class dataToSend
{
public string name { get; set; }
public object value { get; set; }
public List<Args> args = new List<Args>();
}
public class Args
{
public string key { get; set; }
public object value { get; set; }
}
很確定山姆在這裏。有關更多信息,請參閱http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/。假設字符串修復了它,我認爲它會成爲第一個語法。 – 2009-06-30 12:21:18