简体   繁体   中英

How to Pass multiple properties from ajax post call to aspx page webmethod?

I am using below code , but I am not getting value of the object myObj . I am not using MVC. I used simple asp.net(C#) here.

  • Class file

     public class MyClass { public string title { get; set; } public string songPath { get; set; } } 
  • .aspx page

     [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> 

Please let me know what I missed here. Please give me suggestions. Thanks

what if you use multiple parameters in webMethod and then create your object in that method?

[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属性

data: { "myObj": { title: "song title etc...", songPath: "song path edc..." } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM