[英]Could not receive AJAX POST data in PHP
I'm trying to post a variable as an object to my PHP file, but it doesn't receive any data. 我正在尝试将变量作为对象发布到我的PHP文件中,但是它不接收任何数据。 I tried window.alert(u.un)
to test whether the data is being passed from AJAX call, and it works fine and there are no errors in my console. 我试过window.alert(u.un)
来测试是否通过AJAX调用传递了数据,并且它工作正常,并且控制台中没有错误。 But still I'm not getting data in PHP file, there are no errors either. 但是仍然我没有在PHP文件中获取数据,也没有错误。
This is my AJAX function 这是我的AJAX功能
function getfulldetails(n)
{
var u={un:n}; window.alert(u.un);
var locationto= "getfull.php";
$.ajax({
type: "POST",
url: locationto,
data: u,
processData: false,
contentType: false,
success: function(response)
{
window.alert(response);
}
});
return false;
}
This is my PHP file 这是我的PHP文件
<?php
session_start();
if($_SERVER['REQUEST_METHOD']==='POST')
{
if(isset($_REQUEST["un"]))
{
function validate_data($data)
{
require 'connectcred.php';
$data = trim($data);
$data = stripslashes($data);
$data = strip_tags($data);
$data = htmlspecialchars($data);
$data = mysqli_real_escape_string($conn,$data);
return $data;
}
$u=validate_data($_REQUEST["un"]);
echo $u;
}
else
{
echo "something's wrong";
}
}
?>
I'm getting result only from the else
part. 我只从else
部分得到结果。
I've used AJAX using the code below many times to get data from Form and it worked like a charm, but its not working when I assign an object myself. 我已经多次使用AJAX使用下面的代码从Form中获取数据,并且它像一种魅力一样工作,但是当我自己分配一个对象时,它无法工作。
Don't set Content-Type
/ contentType
to false
. 不要将Content-Type
/ contentType
为false
。 You also need to send an actual query string. 您还需要发送实际的查询字符串。
For now, this should fix it: 现在,这应该解决它:
$.post(locationto, u, (response) => {
window.alert(response);
});
Alternatively, use data: "un=" + n
或者,使用data: "un=" + n
Also, in your PHP, use only $_POST
. 另外,在您的PHP中,仅使用$_POST
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.