[RESOLVED]Using an Javascript function to execute an Ajax

This is reaching the ajax portion but it is giving this error

Error Code: Status: [object Object] ex: Error

Code:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
        function GetDateTime() {
            var jsonid = $("#<%= TextBox1.ClientID %>").val();
            var json1 = "{ id: " + jsonid + " }";
            //var  = Sys.Serialization.JavaScriptSerializer.serialize(obj); 
            $.ajax
            ({
                type: "POST",
                url: "Default.aspx/GetServerDateTime",
                data: JSON.stringify(json1),
                contentType: "application/json;charset=utf-8",
                dataType: JSON.stringify(json1),
                success: function (result) {
                    alert(result.d);
                },
                error: function (status, ex) {
                    alert("Error Code: Status: " + status + " Ex: " + ex);
                }
            });
        }
    </script>
        

        protected void Button1_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "Javascript:GetDateTime();", true);
        }
        [System.Web.Services.WebMethod]
        public static string GetServerDateTime(string id)
        {
            string datetimeid1 = "ID: " + id + " Date&Time" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
            return datetimeid1;
        }

Hi Philosophaie,

You can not set the dataType like below:

Philosophaie

dataType: JSON.stringify(json1),

You need to change code like below:

  dataType: "json",

There are four types for dataType:

Intelligent Guess (xml, json, script, or html)

I suggest that you can refer to the link below:

http://api.jquery.com/jquery.ajax/

Best Regards,

Kevin Shen.

first refer Kevin’s reply

and

change following

Philosophaie

data: JSON.stringify(json1),

to

data:{id:jsonid}

Leave a Reply