我正在使用以下代码通过使用jQuery ajax来调用Web服务。但这行不通吗? Web服务以JSON格式返回值。如何通过此代码访问Web服务?

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

最佳答案

也许你可以试试这个。

 $.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服务资料:

[WebMethod]
public string SavePersonById(string ID)
    {
    //do some code here..
    dbContext.Save(ID,"Firstname","Lastnmae");
    return "Successfully Saved!";
    }

09-25 15:22
查看更多