开发者

Remote call ASP.Net Web Service from jQuery Ajax

I have the following problem, there is one ASP. NET Web Service project (*. asmx), with several function and is the second project, also ASP .NET, which should call a service using jQuery Ajax (no service reference). Web Service project:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class MathService : System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod]
    public int Add(int x, int y)
    {
        return x + y;
    }
}

and

<script type="text/javascript" src="Scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: "POST",
            url: "http://localhost:47204/Services/MathService.asmx/Add",
            data: "{'x': '5', 'y': '10'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (res) {
                $("#divMathService").text(res.d);
            },
            error: function () {
                alert("ALARM!");
            }
        });
    });
</script>

This - localhost:47204 - host web service project, when i debug project. I specifically pointed out the full URL, something to use it in the client project. Project works, everything all right. The project client looks as follows:

<script type="text/javascript" src="Scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
       $.ajax({
        开发者_运维百科    type: "POST",
            url: "http://localhost:47204/Services/MathService.asmx/Add",
            data: "{'x': '5', 'y': '10'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (res) {
                $("#divMathService").text(res.d);
            },
            error: function () {
                alert("ALARM!");
            }
        });
    });
</script>

But this project don't show any result, only alert, I don't understand why. Maybe problem with permission for remote call?

I can't attach project, but can send to email if someone wants to look at the project.


try formatting your data as valid json first:

'{"x": 5, "y": 10}'

also, your method needs to know to send the response as json:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public int Add(int x, int y)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜