gpt4 book ai didi

JQuery、ASMX 和格式化 JSON

转载 作者:行者123 更新时间:2023-12-01 07:32:26 25 4
gpt4 key购买 nike

我有以下 JS

  $('#Submit').click(function () {
var name = $('#Name').val();
var age = $('#Age').val();
var phonenumber = $('#PhoneNumber').val();
var isMale = $('#IsMale').val();
var formdata = "'formdata':{name:'" + name + "',age:'" + age + "',phonenumber:'" + phonenumber + "',ismale:'" + isMale + "'}";
$.ajax({
type: "Post",
url: "WebService/FormSubmission.asmx/SavePafForm",
data: formdata,
dataType: "json",
contentType: "application/json; charset=utf-8",
processdata: true,
success: function (msg) { alert(msg.d); },
error: function (result, errortype, exceptionobject) { alert('Error:' + result.responseText); }

})

我有以下 asmx 方法

 [WebMethod]
public string SavePafForm(string formdata)
{
string test = formdata;

return test;
}

我收到以下错误

Error:{"Message":"Invalid JSON primitive: \u0027{name:\u0027tim\u0027,age:\u0027\u0027,phonenumber:\u0027\u0027,ismale:\u0027on\u0027}\u0027.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

当我将 json 字符串更改为此时

var formdata = "{name:'" + name + "',age:'" + age + "',phonenumber:'" + phonenumber + "',ismale:'" + isMale + "'}";

我的网络方法有这个签名

public string SavePafForm(string name, string age, string phonenumber, string ismale)

我没有收到任何错误。

我的目标是向此方法传递最多 20 个参数...我想将其作为我可以处理的一个对象传递,而不是方法签名中的 20 个参数。

有什么想法吗?

编辑:执行标记答案所说的所有操作,除了更改客户端 json 对象来执行此操作

var formdata = JSON.stringify({ formdata: { name: $('#Name').val(), age: $('#Age').val(), phonnumber: $('#PhoneNumber').val(), ismale: $('#IsMale').val()} });

最佳答案

因此,您可以定义一个类型:

public class Foo
{
public string Name { get; set; }
public string Age { get; set; }
public string PhoneNumber { get; set; }
public string IsMale { get; set; }
}

让您的 Web 方法将此对象作为参数:

[WebMethod]
public string SavePafForm(Foo foo)
{
...
}

然后调用:

$.ajax({
type: 'POST',
url: 'WebService/FormSubmission.asmx/SavePafForm',
data: JSON.stringify({
name: $('#Name').val(),
age: $('#Age').val(),
phoneNumber: $('#PhoneNumber').val(),
isMale: $('#IsMale').val()
}),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (msg) {
alert(msg.d);
},
error: function (result, errortype, exceptionobject) {
alert('Error:' + result.responseText);
}
});

关于JQuery、ASMX 和格式化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5047456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com