AJAX没有将值传递给PHP

时间:2022-09-25 15:55:31

I can't find a solution to this simple problem of accessing variable passed to my PHP script via AJAX. I have even tried isset($_POST) but it still fails to find the username and password variables.

我找不到解决这个访问通过AJAX传递给我的PHP脚本的变量的简单问题的解决方案。我甚至尝试过isset($ _ POST),但仍然无法找到用户名和密码变量。

Here is the AJAX call:

这是AJAX调用:

var u = $("#username", form).val();
var p = $("#password", form).val();

//testing
console.log('Username: '+u); // 'John'
console.log('Password: '+p); // 'test'

if(u !== '' && p!=='') {

    $.ajax({url: 'http://www.domain.net/php/user-login.php',
    data: {username:u,password:p},
        type: 'post',                   
    async: true,
    dataType:'json',
    beforeSend: function() {
        // This callback function will trigger before data is sent
        $.mobile.loading('show'); // This will show ajax spinner
    },
    complete: function() {
        // This callback function will trigger on data sent/received complete
        $.mobile.loading('hide'); // This will hide ajax spinner
    },
    success: function (data) {
        //save returned data to localStorage for manual button toggling later
        //window.localStorage.setItem('topGenderDataArray',data);
        console.log("Login successful: "+ data);
        console.dir(data);

        alert("Welcome back "+data['username']);
        $("#loginButton").removeAttr("disabled");
    },
    error: function (xhr,request,error) {
        // This callback function will trigger on unsuccessful action   
        alert('Network error has occurred please try again! '+xhr+ " | "+request+" | "+error);
    }
});

Here is the beginning of the PHP script:

这是PHP脚本的开头:

if(isset($_POST['username']) && isset($_POST['password']))
{
    $data['username'] = $_POST['username'];
    $data['password'] = $_POST['password'];
}
else{
    $data['message']= "Sorry, an error occurred! []";
    $data['user_id']= -1;
    echo json_encode($data);
    exit();
}

3 个解决方案

#1


6  

The problem is in your php code you do the echo json_encode($data) inside the else clause, while you should do it after the if and else just like this:

问题是在你的PHP代码中你在else子句中执行echo json_encode($ data),而你应该在if之后执行它,就像这样:

if(isset($_POST['username']) && isset($_POST['password']))
{
    $data['username'] = $_POST['username'];
    $data['password'] = $_POST['password'];
}
else
{
    $data['message']= "Sorry, an error occurred! []";
    $data['user_id']= -1;
}
echo json_encode($data);

#2


1  

This contains your answer:

这包含你的答案:

How to get body of a POST in php?

如何在PHP中获取POST的正文?

you are postin json data. User and password are not in the _POST array. They are in the request body. You need to json parse it

你是postin json数据。用户和密码不在_POST数组中。他们在请求机构中。你需要json解析它

#3


1  

You just need to print the $data after the if and else statement

您只需要在if和else语句后打印$ data

Like this:

<?php
if(isset($_POST['username']) && isset($_POST['password']))
{
    $data['username'] = $_POST['username'];
    $data['password'] = $_POST['password'];
}
else
{
    $data['message']= "Sorry, an error occurred! []";
    $data['user_id']= -1;
}
echo json_encode($data);
exit();
?>

After is will work properly.

之后会正常工作。

#1


6  

The problem is in your php code you do the echo json_encode($data) inside the else clause, while you should do it after the if and else just like this:

问题是在你的PHP代码中你在else子句中执行echo json_encode($ data),而你应该在if之后执行它,就像这样:

if(isset($_POST['username']) && isset($_POST['password']))
{
    $data['username'] = $_POST['username'];
    $data['password'] = $_POST['password'];
}
else
{
    $data['message']= "Sorry, an error occurred! []";
    $data['user_id']= -1;
}
echo json_encode($data);

#2


1  

This contains your answer:

这包含你的答案:

How to get body of a POST in php?

如何在PHP中获取POST的正文?

you are postin json data. User and password are not in the _POST array. They are in the request body. You need to json parse it

你是postin json数据。用户和密码不在_POST数组中。他们在请求机构中。你需要json解析它

#3


1  

You just need to print the $data after the if and else statement

您只需要在if和else语句后打印$ data

Like this:

<?php
if(isset($_POST['username']) && isset($_POST['password']))
{
    $data['username'] = $_POST['username'];
    $data['password'] = $_POST['password'];
}
else
{
    $data['message']= "Sorry, an error occurred! []";
    $data['user_id']= -1;
}
echo json_encode($data);
exit();
?>

After is will work properly.

之后会正常工作。