Ajax fails to send data on asp.net

0

On the page Puzzle.aspx ego the following button:

  <figure><asp:ImageButton id="puzzle1" runat="server" ImageUrl="~/images/puzzle1.jpg" class="img-rounded img-responsive image" OnClientClick="showModel('uno'); return false" /></figure>

That invokes the function showModel passing it as a parameter String "one".

Function statement:

    <script type="text/javascript" >
    function showModel(variable) {
        $.ajax({
            type: "POST",
            url: "Puzzle.aspx/addSession",
            data: "{'hotelcode':'" + variable + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert("ok");
                $('.modal').modal();
            },
            error: function (err) {
                alert("error");
            }
        });

        return false;
    }
</script>

In the case of success, it should show a modal, in the case of failure, it would show an alert with the message "error".

Code c # of the page Puzzle.aspx :

[WebMethod(EnableSession = true)]
public static void addSession(String hotelcode)
{
    HttpContext.Current.Session["tipoPuzzle"] = hotelcode;

}

It always shows me the alert with error, it never goes by the success to show the modal window.

Can anyone help me with this problem please?

    
asked by Oren Diaz 27.04.2018 в 18:54
source

2 answers

1

The error is here:

data: "{'hotelcode':'" + variable + "'}",

It should be:

data: '{"hotelcode":"' + variable + '"}',
    
answered by 27.04.2018 в 19:22
1

In the end I could solve it:

In the developer's browser console I was throwing error 401 not authorized

Ajax stayed like this:

<script type="text/javascript" >
    function showModel(variable) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: '<%= ResolveUrl("Puzzle.aspx/addSession") %>',
            data: '{"hotelcode":"' + variable + '"}',
            dataType: "json",
            success: function (msg) {
                //alert("ok");
                $('.modal').modal();
            },
            error: function (err) {
                alert("error: ", err);  
            }
        });

        return false;
    }
</script>

And within ~/App_Start/RouteConfig.cs change the line

settings.AutoRedirectMode = RedirectMode.Permanent;

a

settings.AutoRedirectMode = RedirectMode.Off;
    
answered by 27.04.2018 в 21:36