将 JavaScript 对象从 jQuery AJAX 传递到 Web 方法以进行自定义序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5691562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing JavaScript object from jQuery AJAX to web method for custom serialization
提问by Joy
I have a problem in passing a JavaScript object to webmethod in asp.net.
我在将 JavaScript 对象传递给 asp.net 中的 webmethod 时遇到问题。
The JavaScript object is:
JavaScript 对象是:
var Materials = new Object();
function() {
Materials.MaterialName = $('[id*="txtMaterialName"]').val();
Materials.QuantityType = $('[id*="txtquantity"]').val();
AddNewMaterialToDb(Materials);
$(this).dialog('close');
}
Here materials is the object and now I want to pass it to a web method which takes a parameter of class type.
这里的材料是对象,现在我想将它传递给一个接受类类型参数的网络方法。
Now I have two option:
现在我有两个选择:
- either to define the webmethod to take a parameter of MaterialEntity class which would automatically understand the JSON string passed from the AJAX method
- to create the webmethod to take the JSON string and serialize into MaterialEntity class
- 要么定义 webmethod 以获取 MaterialEntity 类的参数,该类将自动理解从 AJAX 方法传递的 JSON 字符串
- 创建 webmethod 以获取 JSON 字符串并序列化为 MaterialEntity 类
How to do that when I am using jQuery AJAX?
当我使用 jQuery AJAX 时怎么做?
I mean to be specific how should I pass the jQuery object as data for jQuery AJAX so that any of the above two conditions gets satisfied?
我的意思是具体我应该如何将 jQuery 对象作为 jQuery AJAX 的数据传递,以便满足上述两个条件中的任何一个?
Function for jQuery AJAX:
jQuery AJAX 函数:
function AddNewMaterialToDb(materials) {
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'Services/Service.asmx/AddNewMaterial',
data :'{"Materials":"' + JSON.stringify(materials).replace('"', '\\"') + '"}',
dataType: "json",
success: function(data, textStatus) {
if (textStatus == "success") {
if (data.d == true) {
alert('New Item Added');
}
}
},
error: function(data, textStatus) {
alert('An error has occured retrieving data!');
}
});
}
回答by Hari Pachuveetil
回答by Naveed Ahmad
you can do something like this:
你可以这样做:
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'Services/Service.asmx/AddNewMaterial',
data : { name: materials.MaterialName, quantity: materials.QuantityType } ,
success: function(data, textStatus) {
if (textStatus == "success") {
if (data.d == true) {
alert('New Item Added');
}
}
},
error: function(data, textStatus) {
alert('An error has occured retrieving data!');
}
});
Now in your asp.net Web method you can simple use Request.Form["name"]
to get the material name and Request.Form["quantity"]
to get quantity type. This way your web method will become generic and you wouldn't have to pass any parameters to your web method.
现在在您的 asp.net Web 方法中,您可以简单地使用Request.Form["name"]
来获取材料名称和Request.Form["quantity"]
数量类型。这样您的 Web 方法将变得通用,并且您不必将任何参数传递给您的 Web 方法。