JQuery Ajax is sending GET instead of POST

前端 未结 11 501
野性不改
野性不改 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:52

    I had this issue, and it turned out to be a URL Rewrite module in IIS.

    I'm using ASP.NET MVC and WebAPI. I created rule to force lowercase URLs so social networks don't view the same URL as two different pages.

    For example:

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

    vs

    "http://url.com/view/something/123guididsomething"

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

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

    I had this issue and per @FAngle's suggestion it was because my .htaccess was removing trailing slashes — and I had set the url to /ajax/foo/bar/ and not/ajax/foo/bar. The redirect changes the request from POST to GET. Remove the / and problem solved!

    0 讨论(0)
  • 2020-12-03 01:02

    I noticed this behavior as well where my POST was sending a GET. The scenario's pretty unique, but maybe it will help someone.

    This was happening to me on my User Role-edit page, where I was using ajax (post) as an immediate action when a role was checked or unchecked.

    I also had the server configured to re-authenticate the user (and redirect them) whenever their role information changed so that their claims would be refreshed.

    The brutal cycle ended up as:

    1. First Role Update -- POST -- 200 success

    2. Next Role Update -- POST -- 302 Found -> Redirect (I did not notice this until I used Fiddler rather than Chrome's network monitor)

    3. Redirect Call from (2) (Same URL) -- GET -- 404 Not Found (since I only allowed Post)

    4. GOTO (1)

    I ended up changing the server to bypass re-authentication/claims update when it detected an ajax request (based on the Accept Types).

    0 讨论(0)
  • 2020-12-03 01:03

    Some problem on MVC. For some reason when I remove the [HttPost] it works as expected, even though I am telling ajax to use POST .

    • It turns out you need to use

    type: "POST"

    • Even though the example on jQuery page says to use

    method : "POST"

    Now it POST's

    But after digging in the documentation I found this.

    enter image description here

    0 讨论(0)
  • 2020-12-03 01:05

    I had a similar issue and it started working for me as soon as I removed the 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"
    });
    
    0 讨论(0)
提交回复
热议问题