Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
HTML
<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>
C#
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;
}
Posted
Comments
Sergey Alexandrovich Kryukov 21-Oct-14 20:22pm    
How can anyone tell you what's the problem, without knowing what server side does?
All right, very smart, your server returns you some object, so what? Some property "d"? If you just alert it, what would you expect?
—SA
Teledextri 21-Oct-14 21:23pm    
The button and the RegisterClientScriptBlock is the trigger but the ServerSide is the [WebMethod] and public static GetServerDateTime.

1 solution

Hi,

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

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

               }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900