Relative Path Problems in Javascript Ajax call

后端 未结 5 732
深忆病人
深忆病人 2020-12-10 02:14

Okay, I have a JavaScript file with the following functions:

function AskReason() {
    var answer = prompt(\"Please enter a reason for this action:\", \"\")         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 02:58

    I have the same issue with ASP.NET MVC with my AJAX call on a separate .js file. This is how it looks:

     return $.ajax({
                type: "POST",
                url: '/Dashboard/Execute',
                contentType: "application/json; charset=utf-8",
                data: filter,
                dataType: "json",
            });
    

    This works fine on my local, of course. But when deployed on a subdirectory in IIS, e.g.

    wwwroot/appsite/subdomainfolder/
    

    This will trigger 404 Not Found as it didn't attach the subdomainfolder on the URL.

    If I remove the

    "/"

    at the beginning of the URL, it will generate it like this one:

    http://localhost/subdomainfolder/Dashboard/Dashboard/ExecuteReader
    

    Which again will trigger the 404 Not Found issue.

    So here are the two options for my workaround:

    Remove the backslash and remove the name of the controller (Dashboard on this case):

    return $.ajax({
                type: "POST",
                url: '/Execute',
                contentType: "application/json; charset=utf-8",
                data: filter,
                dataType: "json",
            });
    

    Or, stay as it is, just add double period at the beginning of the URL:

    return $.ajax({
                type: "POST",
                url: '../Dashboard/Execute',
                contentType: "application/json; charset=utf-8",
                data: filter,
                dataType: "json",
            });
    

提交回复
热议问题