The following code triggers a GET instead of a POST HTTP request.
function AddToDatabase() {
this.url = \'./api/add\';
}
AddToDatabase.prototype.postData
The URL './api/add'
actually got redirected to './api/add/index.php'
. therefore this bizarre side-affect which the new request after the redirect sent using GET
instead of POST
Solution
'./api/add/index.php'
'./api/add/'
.a very common mistake is that we are using button type as submit and not changing the method for form (which is get by default)
make sure you don't use button type submit and if you do you have changed the form method to post
For me, your piece of code look OK, 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/
Check out your .htaccess file or search for some other thing that may redirect your request
I found that when using dataType: 'jsonp'
it converts the request to a GET
. I changed it to dataType: 'json'
it changed from GET
to POST
.
I had the same problem and found this question but answers didn't solve my problem. I eventually solve it by removing contentType
field in ajax request.
contentType: "application/json",