如何从Ajax命令中获取WebMethod [英] How do I get a WebMethod to be enacted from an Ajax Command

查看:84
本文介绍了如何从Ajax命令中获取WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Ajax命令中获取WebMethod?



当我点击Button1时,我收到警告上的错误代码说:

How do I get a WebMethod to be enacted from an Ajax Command?

When I click Button1 I get the Error Code on the Alert saying:

"Error Code: [object Object]"



代码:


Code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
        function GetDateTime() {
            $.ajax
            ({
                type: "POST",
                url: "Default.aspx/GetServerDateTime",
                data: {'one':'1'},
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (result) {
                    alert(result.d);
                },
                error: function (err) {
                    alert("Error Code: " + err);
                }
            });
        }
    </script>

protected void Button1_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "Javascript:GetDateTime();", true);
}
[WebMethod]
public static string GetServerDateTime()
{
    string date = DateTime.Now.ToString();
    return date;
}

推荐答案

.ajax
({
type: POST
url: Default.aspx / GetServerDateTime
data:{' one'' 1'},
contentType:< span class =code-string> application / json; charset = utf-8
dataType: json
成功: function (结果){
alert(result.d);
},
错误: function (错误){
alert( 错误代码: + err);
}
});
}
< / 脚本 >
.ajax ({ type: "POST", url: "Default.aspx/GetServerDateTime", data: {'one':'1'}, contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { alert(result.d); }, error: function (err) { alert("Error Code: " + err); } }); } </script>

protected void Button1_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "Javascript:GetDateTime();", true);
}
[WebMethod]
public static string GetServerDateTime()
{
    string date = DateTime.Now.ToString();
    return date;
}


您好,



尝试更改错误处理部分如下,你可以看到实际的错误信息;



Hi,

Try changing your error handling section as below and you can see the actual error message;

error: function (xhr, status, ex) {
                   alert("error: " + status + ex);

               }


这篇关于如何从Ajax命令中获取WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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