gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:39 25 4
gpt4 key购买 nike

我知道这看起来太复杂了,但我很困惑。

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

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

API 看起来像这样:

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

这在我的本地 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>

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

最佳答案

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

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

关于从 AJAX 请求中剥离 POST 的 PHP 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25250619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com