1

I am trying to achieve a simplest task through ajax using web method . My web method as follow

[WebMethod]
    public static string GetDate()
    {
        return string.Format("says {0}", DateTime.Now.ToString("r"));
    }

and ajax code as follow

 $(document).ready(function() {
            $("#Result").click(function() {
                alert('Result Clicked');

                $.ajax(
               {
                   type: "POST",
                   url: "test1.aspx/GetDate",
                   data : "{}",
                   contentType: "application/json",
                   dataType: "json text",
                   success: function(rsp) {

                       alert('success');
                       alert(rsp);
                       alert(rsp.d);

                       $('#Result').append(rsp.d);


                   },
                   error: function(rsp) {
                       alert(rsp.status + " " + rsp.statusText + "</br>" + rsp.responseText);
                       console.log(rsp);
                       console.log(rsp.responseText);

                   }
               });
            });

        });

but status says OK and 200 status code, but instead of simple string in rsp.d its shows complete HTML of that page self.

1
  • Did you try with data: null or dataType: text ? Does your web.config contain entry for System.Web.Extensions under <httpHandlers> and <httpModules> ? Commented May 2, 2013 at 12:02

1 Answer 1

2

You Can Try this Code May be it is Help Full.

$("#Result").click(function () {
                alert('Result Clicked');
                $.ajax(
                    {
                   type: "POST",
                   url: "Default.aspx/GetDate",
                   contentType: "application/json; charset=utf-8",
                   dataType: "json",
                   success: function (rsp) {
                       alert('success');
                       alert(rsp);
                       alert(rsp.d);
                       $('#Result').append(rsp.d);
                   },
                   error: function (rsp) {
                       alert(rsp.status + " " + rsp.statusText + "</br>" + rsp.responseText);
                   }
               });
            });
Sign up to request clarification or add additional context in comments.

4 Comments

contentType: "application/json; charset=utf-8", dataType: "json", Please Read about Jquery ajax From api.jquery.com/jQuery.ajax
I tired this also same result
Give me your email Id I will send you Demo application
Please Check your mail i sent you on [email protected]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.