0

I am trying to call SendInfo method by clicking on some label from codebehind file for aspx with jQuery.ajax. But all the time it goes to 'error' part, not success, and string isn't returning from method.

So tell me, please, what am i doing wrong here?

just started to learn ajax.

Index.aspx :

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type='text/javascript' src='../../Scripts/jquery-1.4.1.min.js'></script>
<script type="text/javascript" >
    $(document).ready(function () {
        $("#Result").click(function () {
            $.ajax({
                type: "POST",
                url: "Index.aspx/SendInfo",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $("#Result").text(msg.d);
                },
                error: function () {
                    $("#Result").text("adas");
                }
            });
        });
    });
</script>
<div id="Result">Click here for the time.</div>
</asp:Content>

Codebehind file :

public class Index : ViewPage
{
    [WebMethod]
    public static string SendInfo()
    {
        return "Info actually sended";
    }
}
4

1 回答 1

0

At a quick glance I would say it might be due to the "dataType" parameter being set to "json". As this tells it you are expecting a JSON object back, so it will try to parse the response as such and consequently fail due to it being plain-text.

You can just remove the "dataType" parameter completely (and also the empty "data" one) and jQuery will hazard a guess about the content returned and should in this case correctly predict that the response is plain-text.

You may also need to specify the fully qualified URL of your AJAX location and change the "msg.d" in the success function to just be "msg", though this depends on the particular options set on the server (the ".d" is a security feature introduced by Microsoft).

于 2013-03-30T14:31:25.100 回答