0

I'm using the following code to call the webservice by using jQuery ajax. But It doesn't work? The webservice return the value in JSON format. How can I access the webservice through this code?

<html>
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    <script>
    $(document).ready(function () {
        $('input[id^="button"]').click(function () {
            alert('You have clicked ' + $(this).val());
            $.ajax({
                type: 'Get',
                url: 'http://localhost:56789/xxx/Handler.ashx?key=yyy ',
                success: function (data) {
                    alert(data);
                }
            });

        })
    })
    </script>

     <body>
        <div id="Sample_Div">
            <input type="button" id="button1" value="button1" />
        </div>
    </body>
</html>
9
  • This works for me. What error do you get? Commented Apr 23, 2012 at 12:22
  • Am not getting any error at the same time am not getting result Commented Apr 23, 2012 at 12:25
  • 1
    What does the Javascript Console say? Commented Apr 23, 2012 at 12:27
  • Add type="text/javascript" to the script tag to get rid of that warning. Add the code for Handler.ashx. Is the request getting to your handler or is the handler not returning a valid response. Commented Apr 23, 2012 at 12:56
  • Handler give the response but I use the same URL in ajax i didn't get the value Commented Apr 23, 2012 at 13:03

2 Answers 2

2

Maybe you can try this one.

 $.ajax({
        url: "../Services/Person.asmx/SavePersonById",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        type: "POST",
        data: '{ID:"00123"}',
        success: function (response) {
            //do whatever your thingy..
    }
});

Web Service Stuff:

[WebMethod]
public string SavePersonById(string ID)
    {
    //do some code here..
    dbContext.Save(ID,"Firstname","Lastnmae");
    return "Successfully Saved!";
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this:

$(document).ready(function () {
    $('#button').click(function () {
        $.ajax({
            type: "POST",
            url: "appWebservices/select.asmx/checkLogin",
            data: "{ ID:'" + $(this).val()+ "'}",
            contentType: "application/json;charset=utf-8",
            datatype: "json"
         });
     });
});

Write Web services as follow:

[WebMethod]
public string checkLogin(string ID)
{
    //Write your code here..
    //return value
}

know moredetails

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.