C# 通过 jquery ajax 将对象传递给 WebMethod
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15644878/
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 object to a WebMethod through jquery ajax
提问by Abijeet Patro
I am trying to pass an object to a page method defined. I'm trying to pass data to it collected from three textboxes.
我正在尝试将对象传递给定义的页面方法。我正在尝试将从三个文本框收集的数据传递给它。
Page Method
页面方法
[System.Web.Services.WebMethod]
public static string saveDataToServer(object csObj)
{
// Function Body.
}
Javascript/jQuery
Javascript/jQuery
$("#osmSendMsg").click(function () {
debugger;
var cntDetails = {
cntName : $("#osmContactName").val(),
cntEmail : $("#osmContactEmail").val(),
cntMsg : $("#osmContactMessage").val()
}
PostDataToServer(cntDetails,"Please wait...","/ContactUs.aspx/saveDataToServer","csObj");
});
PostDataToServer
发送数据到服务器
// Post data to server (ajax call).
function PostDataToServer(dataToSend, strMessagetoShow, strMethodToCall, jsonObjectName) {
debugger;
/*
dataToSend Contains the JSON Object.
submitType 1 == Normal Submit; 2 == Submit and Print.
strMessagetoShow Text that is displayed in the Please Wait Window.
*/
var tempurl = strMethodToCall;
var tempdata;
$.ajax({
url: tempurl,
type: "POST",
async: false,
dataType: "json",
data: "{" + jsonObjectName + ":" + JSON.stringify(dataToSend) + "}",
//timeout: 30000,
contentType: "application/json; charset=utf-8",
success: function (data) {
tempdata = data;
},
error: function (result) {
tempdata = null;
}
}); //end of the ajax call
return tempdata;
} //End of the post Data
Now the call is reaching the web method. No problem. I'm getting the object as well.But how do I process the object?
现在调用到达 web 方法。没问题。我也得到了这个对象。但是我该如何处理这个对象呢?
As you can see, that's what I'm getting. I also tried declaring a class and passing it as the parameter..but all it's properties are empty. If you notice the data is appearing as a key, value pair. I could convert it into a Dictionary, but I believe that's a complicated solution.
正如你所看到的,这就是我得到的。我也尝试声明一个类并将其作为参数传递......但它的所有属性都是空的。如果您注意到数据显示为键值对。我可以将它转换成字典,但我相信这是一个复杂的解决方案。
A simpler solution would be welcomed!
欢迎使用更简单的解决方案!
回答by spadelives
Your result collection is being returned in the 'data' parameter of your success method. You can simply process this parameter directly like data[0].cntName.
您的结果集合将在您的成功方法的“数据”参数中返回。您可以像 data[0].cntName 一样直接处理此参数。
回答by ViKu
As you are able to reach your webmethod, you need to modify your webmethod to read the data from the object as follows:
由于您能够访问您的 webmethod,您需要修改您的 webmethod 以从对象中读取数据,如下所示:
[System.Web.Services.WebMethod]
public static string saveDataToServer(Dictionary<string, string> csObj)
{
try
{
string Name = csObj["cntName"].ToString();
string Email = csObj["cntEmail"].ToString();
//you can read values like this and can do your operation
return "";//return your value
}
catch(Exception ex)
{
throw new Exception(Ex.Message);
}
}