Why is JQuery AJAX Sending a GET Instead of a POST?

If I take the same amount of time writing this article as I did finding the problem, then this article would probably be 50,000 words long.  However; I also know that articles are supposed to help individuals quickly solve problems and be enjoyable to read therefore; I had to adjust accordingly.  I found this solution after hours on the internet trying to solve a problem that was created when I upgraded the jquery library from 1.6.1 to 1.8.3.  I cannot prove (yet) that Jquery ultimately caused this problem but I can say that when I use any development tool at my disposal I see something quite interesting.  Let’s play a game of what’s different.  Look at following two sets of code and tell me what is different between them.  There is a lot of other code but this is what is important.

CODE A

$.ajax({
    url: "/api/Contact",
    data: JSON.stringify(dataString),
    type: "POST",
    contentType: "application/json;charset=utf-8",
    statusCode: {
        201: function (newMovie) { success(newMovie); },
        400: function (xhr) {
             var errors = JSON.parse(xhr.responseText);
             fail(errors);
             }
       }
  });

CODE B

$.ajax({
    url: "/api/Contact/",
    data: JSON.stringify(dataString),
    type: "POST",
    contentType: "application/json;charset=utf-8",
    statusCode: {
        201: function (newMovie) { success(newMovie); },
        400: function (xhr) {
             var errors = JSON.parse(xhr.responseText);
             fail(errors);
             }
       }
  });

Did you catch it? It is very easy to miss. If you saw it in line 2 then you win.  You see without the ‘/’ at the end no matter what else I tried it sends the request as a GET even if you declare it a POST.  You can see from the screen shots below the result of CODE A and CODE B.  This problem did not rear its ugly head until I upgraded to Jquery 1.8.

CODE A RESULTS

getwithoutslashCODE B RESULTS 

withslashpost1

This proved to take hours the other day to find but 30 seconds to correct.  I hope you enjoyed reading this article while also getting to play a small game.  If not then let me know and I can try and send you my first draft of this article. I have yet to actually find the real reason for this but nevertheless this solved my problem.  Time to move onto the next problem.

Looking for quality web hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under ASP .NET 2, ASP .NET 4.0, Programming/Coding | RSS 2.0

Author Spotlight

David Bauernschmidt

David Bauernschmidt

I live in the historical triangle of Virginia where I am married with two daughters. I have spent over 13 years working for a Fortune 500 company in the computer area. I started in VB 6.0 and by the time I ended my employment I was supervising a development team where we built many web applications. When my first daughter was born I wanted to spend more time with her so I left and became a programmer analyst for local government as well as launch my own company. Since then I have grown James River Webs into a profitable web design and application company helping small businesses create a big presence on the internet. As an employee I have created web application used by citizens and other companies. I enjoy fly fishing, and spending time with my family. I also enjoy learning new approaches and development tools when it comes to developing applications.

2 Comments on Why is JQuery AJAX Sending a GET Instead of a POST?

  1. Stacy Vlasits says:

    Thanks for the article, David. You saved me some serious debugging time.

  2. Luis Matute says:

    I shouldve googled this before spending hours debugging. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *


6 − 4 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>