JQuery Ajax sends GET instead of POST

The following code launches a GET instead of an HTTP POST request.

function AddToDatabase() { this.url = './api/add'; } AddToDatabase.prototype.postData = function(dataToPost) { $.ajax({ type: "POST", url: this.url, data: dataToPost, context: this, success: this.onSuccess }); }; var AddToDatabase = new AddToDatabase(); data = {data: 'coucou'}; AddToDatabase.postData(data); 

Why and how can I get a POST?




I see in Google Chrome Inspect and Firefox. Make sure the browser sends a GET. Here from Chrome:

Request URL: http: // localhost / SAMPLE-CODES / UPDATE% 20MYSQL / api / add / Request Method: GET Status Code: 200 OK




solvable

The URL called "./api/add" was to send a message to. /api/add/index.php. It turns out that calling './api/add /index.php ' or './api/add / ' gives me a POST request.

It was just the wrong URL, but for some reason I was getting a successful GET request for '.api / add /'.

+82
jquery ajax
Aug 30 '12 at 11:29
source share
9 answers

Some problem with MVC. For some reason, when I delete [HttPost], it works as expected, although I tell ajax to use POST.

  • Turns out you need to use

type: "POST"

  • Although the example on the jQuery page uses

method: "POST"

Now it's post

But after digging through the documentation, I found this.

enter image description here

+92
Mar 13 '15 at 14:06
source share

I had this problem and as suggested by @FAngle it was because my .htaccess removed trailing slashes - and I set the URL /ajax/foo/bar/ rather than /ajax/foo/bar . Forwarding changes the request from POST to GET. Remove the problem and solve the problem!

+27
May 05 '16 at 7:09 a.m.
source share

The URL './api/add' really redirected to './api/add/index.php' . therefore this weird side affects the new request after redirecting with GET instead of POST

Decision

  • use the full URL './api/add/index.php'
  • or add the slash './api/add/' .
+9
Oct 17 '16 at 11:19
source share

I noticed this behavior when my POST sent a GET. The script is quite unique, but maybe this will help someone.

This happened to me on the User Role-edit page, where I used ajax (post) as an immediate action when the role was checked or removed.

I also had a server configured to re-authenticate the user (and redirect them) when their role information changed so that their claims were updated.

The brutal cycle ended as follows:

  • First role update - successful POST - 200

  • The next role update is POST - 302 Found → Redirection (I didn’t notice this until I used Fiddler and not the Chrome network monitor)

  • Redirect call from (2) (same URL) - GET - 404 not found (since I only allowed the message)

  • GOTO (1)

I ended up changing the server to bypass reauthentication / claim approval when it detected an ajax request (based on Accept types).

+5
Dec 24 '15 at 18:14
source share

For me, your piece of code looks fine, but if you want to be sure, you can use $ .post instead of $ .ajax

 $.post('ajax/test.html', function(data) { $('.result').html(data); }); 

jquery link: http://api.jquery.com/jQuery.post/

+2
Aug 30 '12 at 11:33
source share

I had a similar problem and it started working for me as soon as I removed hardcoded https:// from my url.

 jQuery.ajax({ type: "POST", url: "www.someurl.com",//instead of "https://www.someurl.com" data: { foo:"bar"}, success: function(d){ console.log(d); }, dataType: "JSONP" }); 
+2
May 03 '16 at 20:31
source share

I had the same problem and found this question, but the answers did not help solve my problem. In the end, I will resolve it by removing the contentType field in the ajax request.

 contentType: "application/json", 
+2
Jun 07 '16 at 19:25
source share

Check the .htaccess file or find one more thing that can redirect your request.

+1
Aug 30 2018-12-12T00:
source share

I had this problem and it turned out to be a URL rewriting module in IIS.

I am using ASP.NET MVC and WebAPI. I created a rule to force lowercase URLs so that on social networks the same URL is not displayed as two different pages.

For example:

" http://url.com/View/Something/123GuidIdSomething "

vs

" http://url.com/View/Something/123GuidIdSomething "

This, however, somehow messed with my ajax requests. I disabled the rule and the problem was resolved.

+1
Jul 28 '16 at 19:22
source share



All Articles