Javascript 如何在jQueryUI对话框按钮调用上调用jQueryAjax函数
我只想在jQueryUI对话框的按钮调用中调用jQueryAjax“POST”请求,下面是代码片段Javascript 如何在jQueryUI对话框按钮调用上调用jQueryAjax函数,javascript,asp.net,jquery-ui,jquery,Javascript,Asp.net,Jquery Ui,Jquery,我只想在jQueryUI对话框的按钮调用中调用jQueryAjax“POST”请求,下面是代码片段 $("#addqust").dialog({ autoOpen: false, width: 630, modal: true, resizable: false, position: { my: "top", at: "top", of: window }, buttons: {
$("#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版本
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js" type="text/javascript"></script>
安装Fiddler并查看流量。您的帖子有错误,或者至少服务器返回500
这里的问题是您的数据对象不是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");
}
}
包括旧浏览器。
默认值。aspx/InsertQuestion
看起来无效。您是否有试图调用的web服务或web api控制器?我在页面前面包含了“[WebMethod()]”method@JasonP-他正在使用ASP.NET AJAX页面方法,该方法使用WebMethod
属性。它本质上是在.aspx
页面中创建一个独立的web服务方法。请在代码隐藏中显示InsertQuestion
方法的代码。啊,好的。在这种情况下,500
通常意味着服务器端出现错误。你试过调试吗?