Tho*_* O. 12 asp.net jquery json webmethod
我试图从jQuery传递一些简单的JSON到ASP.NET 4.5 Webmethod.它并没有像我想要的那样工作.如果我接受输入作为单独的参数,它可以工作:
[WebMethod]
public static Address GetJSonAddress(string name, string street)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试将它作为一个对象,它不起作用,传入的内容只是null:
[WebMethod]
public static Address GetJSonAddress(Address newAddress)
Run Code Online (Sandbox Code Playgroud)
我尝试过使用DataContractJsonSerializer的Webmethods,Pagemethods,WCF ......没什么.Address类使用Datamember/DataContract进行适当修饰.属性匹配包括案例.
jQuery,我在其中尝试了所有传递数据的方式,包括将它包装在Address对象中......如果我以任何其他方式执行它而不是我的Webmethod没有被调用,我得到错误500:
Save2 = function () {
var address = { prefix: GLOBALS.curr_prefix };
$('input[id^=' + GLOBALS.curr_prefix + '],select[id^=' + GLOBALS.curr_prefix + ']').each(function () {
address[this.id.substr(4)] = $.trim($(this).val());
})
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/WebServices/Insert",
data: JSON.stringify(address),
dataType: "json",
success: function (data, textStatus) {
console.log(data, textStatus);
},
failure: function (errMsg) {
MsgDialog(errMsg);
}
});
}
Run Code Online (Sandbox Code Playgroud)
最终我将不得不使用121个输入字符串执行此操作,并且实际上不希望有一个包含121个参数的方法.任何帮助表示赞赏.
Abi*_*tro 41
我快速设置了这个项目,并且我能够成功调用我的Web方法,请相应地调整您的代码.确保您的类属性名称与您通过JavaScript传递的属性名称相同.
网络服务
public static Contact getContact(Contact cnt)
{
cnt.name = "Abijeet Patro";
cnt.phone = "Blah Blah";
return cnt;
}
Run Code Online (Sandbox Code Playgroud)
的JavaScript/jQuery的
$(document).ready(function () {
var cnt = {name:'Hello',phone:'Hello'};
$.ajax({
type: "POST",
url: "/Default.aspx/getContact",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({'cnt':cnt}), // Check this call.
success: function (data) {
debugger;
}
});
});
Run Code Online (Sandbox Code Playgroud)
类
public class Contact
{
public string name { get; set; }
public string phone { get; set; }
}
Run Code Online (Sandbox Code Playgroud)


你可以从这里获取项目.另外,请使用fiddler或Chrome来监控AJAX请求/响应.我添加了一张图片来展示如何使用Chrome监控AJAX请求.提琴手更好更细致.

vin*_*ini -1
I am not aware of ASP.但在 Ruby on Rails 中,我们使用以下过程。我的表单有近 20 个字段。通过serializeArray();我可以将所有输入字段值发送到控制器。喜欢
你的HTML应该是这样的
<input class="input" id="violation_date" type="text" name="violation_date" value=""/>
Run Code Online (Sandbox Code Playgroud)
"name"字段在这里很重要。
var form = $("form#driver_info_form").serializeArray();
var hsh = { }
$.each(form, function(i, e) {
hsh[e.name] = e.value
});
var jqxhr = $.post("/app/DriverInfo/save_driver_info", {
hsh: hsh
}, function() { });
Run Code Online (Sandbox Code Playgroud)
在控制器端我们可以得到类似的参数
{"hsh"=>{"violation_date"=>"date", "violation_time"=>"time", "violation_day"=>"week", "username"=>"name", "address"=>"address", "city"=>"city", "state"=>"state", "zip"=>"", "driver_lic_number"=>"123324", "radio_commercial"=>"Yes", "age"=>"", "birth_date"=>"", "sex"=>"", "hair"=>"", "eyes"=>"", "height"=>"", "weight"=>"", "race"=>""}, "accident_check"=>"false", "misdemeanor"=>"false", "traffic"=>"false", "non_traffic"=>"false", "commercial"=>"Yes"}
Run Code Online (Sandbox Code Playgroud)
由此我们可以访问这些值。
我希望这能给你一些指导。