我想把json对象传递给aspx页面中的WebMethod。
下面是我的jquery和WebMethod
$("#button-login").bind({
Click: accountRegister
});
function accountRegister(e) {
var dataObj = $("#login input[type]").serializeArray();
var obj = JSON.stringify(dataObj);
$.ajax({
type: "POST",
url: "Checkout.aspx/login",
data: obj,
contentType: "application/json; charset=UTF-8",
dataType: "json",
beforSend: function () {
$(this).attr("disabled", "true");
$(this).after(waitObj);
},
success: function (msg) {
// Replace the div's content with the page method's return.
alert("success");
$("#checkout").slideUp("slow");
$("#payment-address").slideDown("slow");
},
error: function (msg) {
//alert(msg);
},
complete: function (jqxhr,status) {
alert("Type:" + $.type(jqxhr) + "\n Respons text: " + jqxhr.responseText + "\n status: " + status);
$(this).attr("disabled", "false");
$(".wait").remove();
},
});
}
我的checkout.cs文件
namespace Ahooratech
{
public partial class Checkout : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string login(string obj)
{ return "{data:data}";}
}
我得到了这个错误:
消息响应文本:{“Message”:“类型\u0027****System.Collections.Generic.IDictionary`2****[System.String,deserialization ,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089,System.Object,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089]\u0027 不支持数组 of an array。”,
“System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList”:“at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object list,类型类型,Boolean序列化程序,Boolean throwOnError,IList\u0026 convertedList)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o,类型类型,Boolean序列化程序,Boolean throwOnError,对象\u0026 convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o,类型类型,JavaScriptSerializer序列化程序,Boolean throwOnError,对象)\r\n在System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer序列化程序,字符串输入,类型类型,convertedObject depthLimit)\r\n在System.Web.Script.Serialization.JavaScriptSerializer.DeserializeT\r\n在System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext上下文,JavaScriptSerializer序列化程序)\r\n在System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context)\r\n at HttpContext context,WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
编辑:错误表示类型强制转换,但是这个转换在哪里?(我认为JQuery中存在隐式类型转换)
EDITE:这是我发送到服务器的json格式的obj内容
"[{"name":"userName","value":"admin"},{"name":"password","value":"admin_"}]"
我把这几行accountRegister()计算成这样:
var obj = JSON.Stringify({data:dataObj});
并在WebMethod中修改为:
公共静态字符串void login(Object data) {
}
而且它起作用了!
但是我怎么才能反序列化数据呢?
https://stackoverflow.com/questions/16648471
复制相似问题