简体   繁体   中英

jQuery $.ajax works with GET but fails on POST

I've got a really weird problem on a customer's server.
I'm using code like the one posted below in several scripts. The $.ajax configurations are almost identical (ie only file, data and success function change) and always use type:POST.
This works most of the times, but for the case below POST always fails. Changing the type to GET works without a problem.

I'm a little bit clueless what happens here.

var parameter = {
    password : $("#password").val()
};

$.ajax({
    type     : "POST",
    url      : "query/submit_password.php",
    dataType : "xml",
    async    : true,
    data     : parameter,
    success  : function(aXHR) { /* ... */ },
    error    : function(aXHR, aStatus, aError) { alert("Error:\n" + aStatus + "\n"+ aError); }
});

This code always results with an alert "NetworkError: A network error occurred.".
Again - other cases with almost identical code work without a problem.
Chrome and Firefox dev tools report a POST error without any further explanation.

Any idea what happens here?


A few more details.

  • The client's site is hosted on GoDaddy
  • The same piece code works well on other servers
  • Yes, the file does exist as a GET request works
  • All browsers I tried this on have no blocker plugins (like adblock) installed

HTTPScoop shows the following results
(3 attempts, the red status says "Connection closed by communications partner"):

HTTPScoop结果

Chrome shows the following:

Chrome错误


Almost solved.
The apache log showed a status 403 on the request.
It also showed a returned size of 0 which probably is the reason why chrome, etc. showed a failed request.
Why this happens is still not clear but it is definitely a server side configuration problem (most likely a mod_rewrite problem or sth. like that).

try add contentType: "application/json; charset=utf-8" to your ajax call

$.ajax({
    type     : "POST",
    url      : "query/submit_password.php",
    contentType: "application/json; charset=utf-8",
    dataType : "xml",
    async    : true,
    data     : parameter,
    success  : function(aXHR) { /* ... */ },
    error    : function(aXHR, aStatus, aError) { alert("Error:\n" + aStatus + "\n"+ aError); }
});

if it doesn't help try what benhowdle89 says, stringify(parameter).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM