jjharrison
jjharrison

Reputation: 871

JavaScript file returns 404 Not Found on IIS

Now I know there are a few questions around with the same problem but I cannot find a solution, bearing in mind I'm fairly new to JavaScript and/or IIS.

So I have some Java script in an MVC app that when run locally via the Visual Studio IIS express, works a treat with no problems. I also have an IIS site that I have published with exactly the same code. Everything works fine apart from the JavaScript. I am including my script in a Razor view like so:

<script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")" type="text/javascript"></script>

And the following JavaScript is what I am trying to run.

<script>
function executeQuery() {
    var url = "/Application/CheckMobileResponse";
    debugger;


    $.get(url, { applicationId: @Model.ApplicationId, userId : @Model.UserId }, function(response) {
        debugger;
        //doing some stuff

    });
    setTimeout(executeQuery, 3000);
}

$(document).ready(function() {
    setTimeout(executeQuery, 3000);
});

Basically I am calling an action every 3 seconds and depending on the response I am using window.location.href to redirect to a new view.

I am quite confused because in the console window on google chrome I am getting a 404 server response Error as follows:

Failed to load resource: the server responded with a status of 404 (Not Found) jquery-1.10.2.js:8720

GET http://localhost/Application/CheckMobileResponse?applicationId=1&userId=5 404 (Not Found)

This to me looks like it is finding the jQuery file so may not be a case of not being able to load the JavaScript file. But it is instead failing because it cannot find my action on the GET request (which is odd because this works perfectly on Visual Studio IIS Express).

Upvotes: 2

Views: 5313

Answers (1)

Shyju
Shyju

Reputation: 218722

404 error means, the url you are trying to access does not exist/not found!

From your comment, your site's base name is localhost/AppDashboard .But when your run your code, It is trying to make a call to http://localhost/Application/CheckMobileResponse. It is missing the AppDashboard ! part in the url.

You should not be hardcoding the path to your action method like that. Always try to use Url.Action helper method to generate the correct url to the action methods. This way, your code will work (will generate the proper url(s)) irrespective of what page/path your user is currently in.

 var url = "@Url.Action("CheckMobileResponse","Application")";

When the page renders, razor will execute the Url.Action method and generate the url. So if you put the above code inside a razor view it will work. But if your js code is inside an external js file, Follow the solution explained in this post.

Upvotes: 3

Related Questions