从AJAX请求剥离POST的PHP配置?

M M*_*ler 5 php ajax jquery post bluehost

我知道这似乎太复杂了,但我很困惑.

我有一个页面使用jQuery的post方法向我的API发送一个AJAX POST请求.它们都在同一个域/服务器上.

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

API看起来像这样:

<?php
exit (json_encode(array ('response' => print_r($_REQUEST, true))));
Run Code Online (Sandbox Code Playgroud)

这在我的本地WAMP设置中按预期工作,但在Bluehost上,它显示就Array ()好像请求中没有参数.

如果我$.post改为$.get,它接收两个参数就好了.

如果我使用HTML表单并在不使用AJAX的情况下发送数据,它也会按预期工作,例如

<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>
Run Code Online (Sandbox Code Playgroud)

我想我已经用尽了我可以创建的测试以试图消除任何其他可能性,它只是归结为一些非常奇怪的东西 - 我的PHP脚本在AJAX请求中没有收到POST字段.

小智 1

因为服务器能够从 HTML 接收 post 值。jQuery post 方法可能有问题。因此,在 jQuery post 上,您可以尝试 ajax 函数来发布数据。替换以下功能并查看天气是否有效。

$.ajax({
type: "POST",
url: "api/login.php",
   data: { username: 'test', password: 'test' }
})
.done(function( res ) {
   alert(res.response);
});
Run Code Online (Sandbox Code Playgroud)