JQuery Ajax is sending GET instead of POST

前端 未结 11 500
野性不改
野性不改 2020-12-03 00:20

The following code triggers a GET instead of a POST HTTP request.

function AddToDatabase() {
  this.url = \'./api/add\';
}

AddToDatabase.prototype.postData          


        
相关标签:
11条回答
  • 2020-12-03 00:43

    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

    • use the full url './api/add/index.php'
    • or add a slash './api/add/' .
    0 讨论(0)
  • 2020-12-03 00:45

    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

    0 讨论(0)
  • 2020-12-03 00:46

    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/

    0 讨论(0)
  • 2020-12-03 00:48

    Check out your .htaccess file or search for some other thing that may redirect your request

    0 讨论(0)
  • 2020-12-03 00:51

    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.

    0 讨论(0)
  • 2020-12-03 00:52

    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",
    
    0 讨论(0)
提交回复
热议问题