如何从ajax post call传递多个属性到aspx页面webmethod
How to Pass multiple properties from ajax post call to aspx page webmethod?
我使用下面的代码,但我没有得到对象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>
请让我知道我错过了什么。请给我一些建议。由于
如果你在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属性
data: { "myObj": { title: "song title etc...", songPath: "song path edc..." } }
相关文章:
- RegEx删除空属性?例如,如果(class=“”||class=“”)移除;否则就下课
- 全局变量和全局对象的属性之间有什么区别吗
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 序列化数据属性中对象的最可靠方法
- 分析高度属性时出现意外值{{specs.height}}.index.html
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- AngularJS-使用'true'属性
- 可以't使用JavaScript获取width属性
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 如何使用javascript获取嵌套对象中所有子对象的单个属性
- JavaScript Pub/Sub属性访问问题
- 从JavaScript访问struts操作中的属性
- 是否可以从父类访问子类的属性
- 如何更改reactjs中外部/独立组件的状态或属性
- 如何在选项卡上定义属性'的主窗口对象
- 将 jQuery 代码添加到 Index.aspx 页面
- 锚点元素的href属性自动更改
- 如何从ajax post call传递多个属性到aspx页面webmethod