加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 02:56:15 所属栏目:百科 来源:网络整理
导读:我知道这似乎太复杂了,但我很困惑. 我有一个页面使用jQuery的post方法向我的API发送一个AJAX POST请求.它们都在同一个域/服务器上. $.post('api/login.php',{username: 'test',password: 'test'}).done(function (res) { alert(res.response);}); API看起来
我知道这似乎太复杂了,但我很困惑.

我有一个页面使用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方法可能存在问题.所以在jQuery帖子上你可以试试ajax函数来发布数据.更换以下功能并查看其工作天气.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读