Video of the week

I've found this great video and I recommend you to watch if you are interested in wireshark and filtering methods

jQuery Not Found (404) controller's action (ASP.NET MVC)

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:

  1. $.ajax({
  2. type: 'POST',
  3. url: '/Home/InsertNumber',
  4. data: JSON.stringify({
  5. Firstname: firstname.trim(),
  6. Lastname: lastname.trim(),
  7. Department: department.trim(),
  8. Telephone: telephone.trim(),
  9. Keywords: keywords.trim()
  10. }),

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?

  1. [HttpPost]
  2. public JsonResult InsertNumber(string firstname, string lastname, string department, string telephone, string keywords)
  3. {
  4. try
  5. {
  6. var client = new MongoClient(_connectionString);
  7. var database = client.GetDatabase(_database);
  8. var collection = database.GetCollection<BsonDocument>(_queue);
  9. // ..
  10.  
  11. 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:

  1. $.ajax({
  2. type: 'POST',
  3. url: '/telefony/Home/InsertNumber',
  4. data: JSON.stringify({
  5. Firstname: firstname.trim(),
  6. Lastname: lastname.trim(),
  7. Department: department.trim(),
  8. Telephone: telephone.trim(),
  9. Keywords: keywords.trim()
  10. }),

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.


Your browser doesn't support this tag cloud