admin管理员组

文章数量:1394544

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>

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>
Share Improve this question edited Apr 23, 2012 at 15:33 noob 9,2124 gold badges39 silver badges65 bronze badges asked Apr 23, 2012 at 12:17 useruser 1893 gold badges8 silver badges18 bronze badges 9
  • This works for me. What error do you get? – binarious Commented Apr 23, 2012 at 12:22
  • Am not getting any error at the same time am not getting result – user Commented Apr 23, 2012 at 12:25
  • 1 What does the Javascript Console say? – binarious 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. – Kevin Junghans 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 – user Commented Apr 23, 2012 at 13:03
 |  Show 4 more ments

2 Answers 2

Reset to default 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!";
    }

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

本文标签: javascripthow to call webservice using Ajax with jqueryStack Overflow