如何在Jquery UI Dialog按钮调用上调用Jquery Ajax函数

我只想在Jquery UI对话框的按钮调用上调用jQuery Ajax“POST”请求,这里是代码片段,

$("#addqust").dialog({ autoOpen: false, width: 630, modal: true, resizable: false, position: { my: "top", at: "top", of: window }, buttons: { "Save": function (e) { $.ajax({ type: "POST", url: "Default.aspx/InsertQuestion", contentType: "application/json; charset=utf-8", data: { name: $("#qst").val() }, success: function (data) { console.log($("#qst").val()); console.log(data.d); // alert("message"); } }).done(function (msg) { alert("Data Saved "); }); }, "Cancel": function () { $(this).dialog("close"); } } }); 

但我收到以下错误或console.log,而不是调用Page Web方法,

 POST http://localhost:50583/Default.aspx/InsertQuestion 500 (Internal Server Error) send v.extend.ajax $.dialog.buttons.Save (anonymous function) v.event.dispatch o.handle.u 

我想知道我犯了什么错误,感谢您的帮助。

更新WebMethod,它是一种简单的测试方法,

 [WebMethod()] public static string InsertQuestion(string name) { try { string strjson; strjson = name; return strjson; } catch (Exception ex) { throw new System.Exception( ex.Message); } } 

使用的jquery版本,

    

安装Fiddler并查看流量。 您的post有错误,或者至少服务器返回500。

http://fiddler2.com/

这里的问题是您的数据对象不是JSON字符串。 在你的ajax调用中尝试这个:

 buttons: { "Save": function (e) { var dataToSend = JSON.stringify({ name: $("#qst").val() }); $.ajax({ type: "POST", url: "Default.aspx/InsertQuestion", contentType: "application/json; charset=utf-8", data: dataToSend, success: function (data) { console.log($("#qst").val()); console.log(data.d); // alert("message"); } }).done(function (msg) { alert("Data Saved "); }); }, "Cancel": function () { $(this).dialog("close"); } } 

包含旧版浏览器的JSON.js。