简体   繁体   中英

JQuery .ajax() function call not working, but success method runs

I'm having trouble getting an ajax call to work on a webpage. The WebMethod works just fine.

One potential problem that I might see is that the code that calls the ajax method is located in a UserControl that is embedded in a content page that lives in a master page. This page can only be accessed after .Net authentication is verified. I have no clue if any of this matter, I'm just including this information so there are no hidden aspects of this issue.

Another potential issue may be in the Ajax call itself. I just can't see what the problem is. Most confusing of all, the success method in the ajax call runs.. there is no failure. More confusing, if I change the name of the WebMethod to something else entirely, the success method of the ajax call still runs.

Any idea what may be going wrong here?

var sendingThis = { x: 'abc', y: 'def', z: 'ghi' }

$.ajax(
{
    type: "POST",
    url: "TestPage.aspx/someMethod",
    data: sendingThis,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: true,
    cache: false,
    success: function (msg)
    {
         //THIS STUFF GETS CALLED...always
    },//end success

    error: function () 
    { 
      //DOESN'T GET CALLED 
    }
});

Codebehind method in TestPage.aspx:

[System.Web.Services.WebMethod]
public static void someMethod(string x, string y, string z) 
{
    //CODE THAT UPDATES DATABASE; *INDEPENDENTLY CONFIRMED WORKING* 
}

The button that calls the code..:

<asp:Button ID="btnSomeMethod" runat="server" OnClientClick="return someMethod(this);" Text="Runit" />

Some additional observations I have that may help or not. I've tried using ResolveURL with the .ajax() call's URL, didn't work. I've also tried EnableSession:true as an attribute for the Web Method. I've tried inserting a ScriptManager into the page with EnablePageMethods="true" and a ScriptReference to the .js file that holds the Ajax() call. No luck whatsoever. What on earth is going on?

Another update: I checked the HTTP stream to see what the Response Header is from the the POST request. First off, it's returning 200 OK as the POST status code. Request seems all good. However, in the response header, I'm getting:

`"status":401,"headers":`

This is related to an authentication error.

The answer came down to two forward slashes // .. In RouteConfig.cs , you need to comment out the line:

settings.AutoRedirectMode = RedirectMode.Permanent;

That's it, that's all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM