0

i'm tried to implement ajax using jQuery.Its my first time to use ajax.so in a button click i need to display the current datetime. For this, i write the below code

//==============aspx page===============

<script type="text/javascript">

    $(document).ready(function (){
        var Button1 = $("#Button1");
        var Label1 = $("#Label1");
        Button1.click(function (){
          $.ajax({
                type: "POST",
                url: "/myownajax14/WebService/WebService1.asmx/GetDateTime",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#Label1").text(msg.d);
                    //alert('hi');
                    },
                error:alert('error');     
            });     

        });
    });

//================asmx.cs page===========

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
    [WebMethod]
    public string GetDateTime()
    {
        return DateTime.Now.ToString();
    }
}

My problem is,it doesn't give the answer..and it doesn't show any error.Have any mistake in this code?Please help me..

8
  • First you should trace your Request using FireBug Commented Feb 18, 2013 at 5:13
  • You can debug this to see exactly where the problem is. For example, while debugging through VS, put a breakpoint in the method and see if it hits. If so, then the javascript to call it is ok, so the problem must be after. Also, look at the Network tab in your browser's developer tools to see if a server request ever actually happens. If not, then the problem is with the code that calls the method. Whatever the result, you'll learn a lot about what the problem is (or isn't) by debugging using your tools. Commented Feb 18, 2013 at 5:13
  • try this: Button1.click(function (e){ e.preventDefault() and use error function error:function(){alert('error')}; Commented Feb 18, 2013 at 5:18
  • @Jai : i tried it.but its not worked.still gave the same result.. Commented Feb 18, 2013 at 5:41
  • @pearl90 try this in success function and look in the console what do you get there: console.log(msg); Commented Feb 18, 2013 at 5:43

3 Answers 3

0

I too had a similar problem it's simple try this:

You need to specify type="button" attribute.

Sign up to request clarification or add additional context in comments.

2 Comments

Here is the same problem what I have experienced stackoverflow.com/questions/10567951/…
Have you gone through my question posted?
0

contentType: "application/json;" means that the client need the server to response a json type data.But the server response a string type one instead.

In the "success" block,the "msg" must be like '2010-10-10',so error happens in msg.d.

try the following for a string type data,or response a json type data in the server code such as {"y":2010,"m":10,"d":10}.

contentType: "application/text; charset=utf-8",
success: function (date) {
    $("#Label1").text(date);
}

Comments

0

Have you tried accessing the ASMX directly using a browser? This way, you can easily see what kind of response your ASMX is producing...

Comments

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.