如何在Jquery UI对话框按钮调用上调用Jquery Ajax函数 [英] How do call an Jquery Ajax function on Jquery UI Dialog button call

查看:105
本文介绍了如何在Jquery UI对话框按钮调用上调用Jquery Ajax函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just want to call a jQuery Ajax "POST" request on Jquery UI dialog's button call, here is the code snippet,

    $("#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方法,

but I'm getting the following error or console.log, and not calling the Page Web method,

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

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

I would like to know what error I made, appreciate your help.

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

Update the WebMethod, its a simple method for testing,

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

使用的jquery版本

jquery versions used,

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

推荐答案

此处的问题是您的数据对象不是JSON字符串.在您的ajax调用中尝试以下操作:

The problem here is that your data object is not a JSON string. Try this in your ajax call:

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 .

这篇关于如何在Jquery UI对话框按钮调用上调用Jquery Ajax函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆