C# 如何将多个属性从ajax post调用传递到aspx页面webmethod?
我正在使用下面的代码,但我没有得到对象的值C# 如何将多个属性从ajax post调用传递到aspx页面webmethod?,c#,javascript,jquery,asp.net,C#,Javascript,Jquery,Asp.net,我正在使用下面的代码,但我没有得到对象的值myObj。我没有使用MVC。我在这里使用了简单的asp.net(C#) 类文件 public class MyClass { public string title { get; set; } public string songPath { get; set; } } .aspx页 [System.Web.Services.WebMethod] public static string PostData(MyClass myOb
myObj
。我没有使用MVC。我在这里使用了简单的asp.net(C#)
- 类文件
public class MyClass { public string title { get; set; } public string songPath { get; set; } }
- .aspx页
[System.Web.Services.WebMethod] public static string PostData(MyClass myObj) { // myObj.title should have value = "song title etc..."; // myObj.songPath should have value = "song path edc..."; return "done"; }
- JS
<script type = "text/javascript"> function PostData() { $.ajax({ type: "POST", url: "CreateLeave.aspx/PostData", data: { title: "song title etc...", songPath: "song path edc..." }, contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, failure: function (response) { alert("error"); } }); } function OnSuccess(response) { alert("success"); } </script>
函数PostData(){ $.ajax({ 类型:“POST”, url:“CreateLeave.aspx/PostData”, 数据:{标题:“歌曲标题等…”,歌曲路径:“歌曲路径edc…”, contentType:“应用程序/json;字符集=utf-8”, 数据类型:“json”, 成功:一旦成功, 故障:功能(响应){ 警报(“错误”); } }); } 函数OnSuccess(响应){ 警惕(“成功”); }
谢谢如果在webMethod中使用多个参数,然后在该方法中创建对象,该怎么办
[System.Web.Services.WebMethod]
public static string PostData(string title, string songPath, //...etc)
{
MyClass myObj = new myClass();
myObj.title = title;
myObj.songPath = songPath;
return "done";
}
尝试在ajax调用中这样更改数据属性
data: { "myObj": { title: "song title etc...", songPath: "song path edc..." } }
您在哪里指定要调用PostData方法?@Neel url:“CreateLeave.aspx/PostData”,在ajax调用中,您的question@Neel哦,这是一个错误,但在我的代码中它被更正了,但仍然不起作用。你应该在WebMethod中返回myObj,但在未来,我将有20到25个属性,那么还有别的办法吗?