Recently I've been working on a small ASP.NET MVC project and I was about to launch it on the production server. Everything seemed just fine until I called one jQuery function from my Scripts\phonebook.js file. The result was very clear - 404 NOT FOUND (btw. developer tools available in either Firefox or Chrome are very helpful in the process of solving these types of network / web development issues). According to what I've found on the web, it's quite common problem but it wasn't that obvious when it came to the solution. Here's a piece of the code:
$.ajax({ type: 'POST', url: '/Home/InsertNumber', data: JSON.stringify({ Firstname: firstname.trim(), Lastname: lastname.trim(), Department: department.trim(), Telephone: telephone.trim(), Keywords: keywords.trim() }),
So when I click my #button on the website and InsertNumber is called then I get NOT FOUND (404) - why? When I run it on IIS Express instance in debug mode, everything is ok but after I publish my app on IIS it doesn't work. Is there something wrong with my InsertNumber method?
[HttpPost] public JsonResult InsertNumber(string firstname, string lastname, string department, string telephone, string keywords) { try { var database = client.GetDatabase(_database); var collection = database.GetCollection<BsonDocument>(_queue); // .. return Json("result");
This method is fine - there must be something wrong with ajax call. It came up that I forgot about one very important thing. When you upload your website on IIS you give it a name - right? I called my app (website) 'telefony' - so when I run it i type: http://server/telefony - so all I had to do to fix my 404 problem was to add telefony before /Home/InsertNumber method call in url parameter: /telefony/Home/InsertNumber.
To make it work like a charm I had to correct this one part only:
$.ajax({ type: 'POST', url: '/telefony/Home/InsertNumber', data: JSON.stringify({ Firstname: firstname.trim(), Lastname: lastname.trim(), Department: department.trim(), Telephone: telephone.trim(), Keywords: keywords.trim() }),
Solution: Let's do not forget about the name of our app deployed on IIS server - it must be included in url parameter when want to call a method from jQuery! You can optionally paste your java code in html and use '@Url.Action('Method', 'Controller')' as the parameter of url - then simply look at the source code. You'll find something like this: url: '/telefony/Home/InsertNumber' - do it when you're not sure about the correct url.