[RESOLVED]Use WebMethod to display Ajax ID and Server Time

I have a file on my server "~/json1.txt" I want to extract using the "Get" method: "{"one":"1", "two":"2", "three":"3"}

Then I want to use WebMethod to display Ajax ID and Server Time.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            function GetDateTime() {
                $.get("json1.txt", function (data) {
                    var json1 = data;
                    $.ajax
                    ({
                        type: "POST",
                        url: "Default.aspx/GetServerDateTime",
                        data: json1,
                        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 dateid = "Date: " + DateTime.Now.ToString();
            return dateid;
        }
Error code:
"JavaScript runtime error: 'GetDateTime' is undefined"

Hi,

What if using just:

 <script type="text/javascript">
// just define the function function GetDateTime() { $.get("json1.txt", function (data) { var json1 = data; $.ajax ({ type: "POST", url: "Default.aspx/GetServerDateTime", data: json1, contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { alert(result.d); }, error: function (err) { alert("Error Code: " + err); } }); }); }
// Called when page is loaded
  $(function () { // Call your function from here rather than to just define a function Inside this anonymous function.
 
GetDateTime();
});
</script>

Else you define your GetDateTime function as a member of an outer anonymous function. What you place here is some code that is intended to automatically run when the page is ready.

Edit: and don’t emit this server side code. It doesn’t have any purpose. Instead do the call Inside your startup function (code above edited).

Got
rid of a few things.

Error
Code: Alert says: "Error Code: [object Object]"

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

 

See https://api.jquery.com/jQuery.ajax/ and scroll down to the error function. For now you are showing the object that does the request so it doesn’t help much. See the status and text or the query response.

Could it be that you pass a parameter while this method doesn’t have any parameter?

Hi Philosophaie,

Thanks for your post.

Philosophaie

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

According to your code, you pass a parameter called “one” to WebMethod, so your WebMethod should could accept this parameter. So your code may look like as below.

[WebMethod]
public static string GetServerDateTime(string one)
{
    //Do some operation according to parameter "one"
    string dateid = "Date: " + DateTime.Now.ToString();
    return dateid;
}

Hope it will be helpful to you.

Best Regards,

Fei Han

Leave a Reply