5

I know this seems way too complicated to be the case, but I'm baffled.

I have a page using jQuery's post method to send an AJAX POST request to my API. They are both on the same domain/server.

$.post('api/login.php', {username: 'test', password: 'test'}).done(function (res) {
    alert(res.response);
});

The API looks like this:

<?php
exit (json_encode(array ('response' => print_r($_REQUEST, true))));

This works as expected in my local WAMP setup, but on Bluehost, it shows simply Array () as if there were no parameters in the request.

If I change $.post to $.get, it receives both parameters just fine.

It also works as expected if I use an HTML form and send the data without using AJAX, e.g.

<form method="post" action="api/login.php">
    <input type="text" name="username" value="test">
    <input type="text" name="password" value="test">
    <input type="submit">
</form>

I think I've exhausted the tests I can create to try to eliminate any other possibility, and it just is coming down to something really strange -- my PHP script doesn't receive POST fields in an AJAX request.

M Miller
  • 5,364
  • 9
  • 43
  • 65
  • 4
    Check your browser's development console to see if it's registering a cross-domain security exception or some other error that's suppressed by the interface. Those kinds of errors are common when moving from a local server to a remote. – acrosman Aug 11 '14 at 19:13
  • I'm in Chrome and I receive no error. Note that `.done()` is firing, which means it received a `2xx` header and a response which is a valid JSON object. No console errors, no PHP warnings/error logs, just an empty request.... – M Miller Aug 11 '14 at 19:15
  • The setup of `$_REQUEST` can be modified in php.ini using the `request_order` directive. Try changing the script to `$_POST` instead of `$_REQUEST` and see if that works. I don't know if your host supports custom php.ini configurations. – cOle2 Aug 11 '14 at 19:24
  • I tried `$_POST` before `$_REQUEST`. I just tried `$_REQUEST` so I could see if the `$.get` version was working. – M Miller Aug 11 '14 at 19:25
  • 1
    Did you verify in the devtools that the parameters were really sent ? – Lorenz Meyer Aug 11 '14 at 19:31
  • Try doing `echo json_encode(array('response' => $_POST));` – adeneo Aug 11 '14 at 19:33
  • @lorenz-meyer: How do I do that? @ adeneo: I already did that, see my earlier comment. – M Miller Aug 11 '14 at 19:51
  • opening the link with parameters like: api/login.php?username=aa&password=bb works as expected when using $_REQUEST ? Else the Jquery may be the fault .. – ion Aug 11 '14 at 19:51
  • @MMiller - you tried exactly like that, without the print_r in the middle ? – adeneo Aug 11 '14 at 19:55
  • You look at the details of the request in the network tab of Chrome's or Firefox' devtools. – Lorenz Meyer Aug 11 '14 at 19:59
  • @adeneo why that long? why not just `print_r($_POST);`? – Sebastien Aug 11 '14 at 20:01
  • I'm seeing a 301 bounce. Could that be responsible? I would think a POST request would be forwarded with a bounce, especially considering it shouldn't be treated any differently than a traditional (non-XMLHttpRequest) request – M Miller Aug 11 '14 at 20:07
  • Why not chain on a `fail` method to the ajax request and see if it throws – adeneo Aug 11 '14 at 20:36
  • It isn't failing because `done` is being called. The two are exclusive. I get a response, it just doesn't have the fields. – M Miller Aug 11 '14 at 20:39
  • Then try something simpler, just `echo "hello"` and see if you get that – adeneo Aug 11 '14 at 20:40
  • Okay, you're not understanding the problem. The *request* functions just fine and returns a perfectly valid *response.* But the *request* does not send POST values correctly. – M Miller Aug 11 '14 at 20:58
  • So it works if you return anything else, just not $_POST, and it works if you send a POST request with a regular form and on localhost with ajax, just not with ajax on Bluehost. Sounds unlikely, but I suppose all you can really do then is contact Bluehost and ask what they're doing. – adeneo Aug 11 '14 at 21:05
  • Interestingly enough, it appears to be an issue of the hosting account having no domain. http://box###.bluehost.com/~USERNAME/ did not work but http://DOMAIN.com/ did (despite there being no absolute URLs in the code anywhere -- in fact, I hadn't even decided on a domain yet). – M Miller Aug 11 '14 at 21:32
  • @MMiller, can you try echo json_encode(array ('response' => file_get_contents("php://input"))); – Aivar Aug 12 '14 at 07:22

1 Answers1

1

As the server is capable of receiving post values from HTML. There may be problem with the jQuery post method. So instead on jQuery post you can try ajax function to post the data. Replace following function and see weather it works.

$.ajax({
type: "POST",
url: "api/login.php",
   data: { username: 'test', password: 'test' }
})
.done(function( res ) {
   alert(res.response);
});
  • If you read the jQuery source, you can see that `$.post` does exactly this. Oddly enough, it was a problem related to a lack of a domain, so it must be something weird in the server configuration (since all the paths were resolving correctly and there were no origin-related errors). – M Miller Aug 16 '14 at 21:06