简体   繁体   中英

Ajax call returning error 401 Unauthorized

I'm developing a web application using HTML and JavaScript (jQuery), for database communication I'm using an .asmx web service in C#.

Deploying my app on local computer and local IIS works fine. The problem is that now I need to deploy my application on company web server and now is when every ajax call doesn't work and returns error 401 . Something weird is that if I enter to the ajax call url (the asmx web service is displayed) and then return to my web site and reload the page now all the ajax calls works. For example:

$.ajax({
   url: "http://192.168.46.87/MyApp/ws_WebService.asmx/Login"

This ajax call returns error 401, after that if I enter to the url http://192.168.46.87/MyApp/ws_WebService.asmx my web service is loaded and if I reload the tab of my web page now ajax calls works fine.

This is my ajax call:

function CallWM(webMethod, JSONToSend, callBackFunction, extraData) {
    try {
        $.ajax({
            type: 'POST',
            url: "http://192.168.46.87/MyApp/" + webMethod,
            contentType: 'application/json',
            dataType: 'json',
            data: JSON.stringify(JSONToSend),
            success: function (data, strErr, xhr) {
                callBackFunction(data.d, extraData);
            },
            error: function (xhr, textStatus, errorThrown) {
                bootbox.alert("Error calling the server!\n" + xhr.statusText);                                
                console.log(xhr.statusText);
                console.log(xhr);
                $.unblockUI();
            }
        });
    }
    catch (err) {
       bootbox.alert("Error calling the server!\n" + err.message);
       $.unblockUI();
    }
}

I already tried changing authentication type on server IIS, sending my credentials with the ajax call but I get the same error message, what other thing can I try or what could be the problem?

PD. On my local IIS works perfectly, the problem is now that I'm moving my app to a web server.

finally I can fix this issue and here is the solution in my case .

On my application path I have an Web.config where I do not have anything related to authentication but I discovered that on IIS inetpub applications folder root there's another Web.config and on that file I saw that there is something about authentication, removing that solve my problem.

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