PHP configuration that passes POST from an AJAX request?

I know this seems too complicated to take place, but I'm puzzled.

I have a page using jQuery's mail method to send an AJAX POST request to my API. They are both located 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 just shows Array () as if there were no parameters in the request.

If I change $.post to $.get , it gets both options just fine.

It also works as expected if I use an HTML form and submit data without using AJAX, for example.

 <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 have exhausted the tests that I can create to try to eliminate any other possibility, and it just comes down to something really strange - my PHP script does not receive the POST fields in the AJAX request.

+5
source share
1 answer

Because the server is able to get post values ​​from HTML. There may be a problem with the jQuery post method. So instead of a jQuery message, you can try an ajax function to publish the data. Replace the following function and see how it works.

 $.ajax({ type: "POST", url: "api/login.php", data: { username: 'test', password: 'test' } }) .done(function( res ) { alert(res.response); }); 
+1
source

Source: https://habr.com/ru/post/1200052/


All Articles