2

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).

5
  • what is your site's base url ? Commented Apr 4, 2016 at 17:58
  • localhost/AppDashboard? Commented Apr 4, 2016 at 18:01
  • Most likely it is a security issue, your web.config might not be allowing the retrieval of .js file extension content from the scripts folder. Alternatively check your IIS folder where you published your project and make sure the script actually exists on disk. Commented Apr 4, 2016 at 18:01
  • A 404 error has nothing to do with client-side code. If the AJAX request is being made then the JavaScript is clearly working. A 404 means that the URL doesn't exist. Should it? Commented Apr 4, 2016 at 18:01
  • Ah okay david, so the 404 means my javascript is definately working. And yes the URL should exist Commented Apr 4, 2016 at 18:04

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Absolute life saver , thankyou... This is making me feel stupid because I have read alot that Url helpers are a must in order to avoid problems such as this. Thankyou again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.