简体   繁体   English

AngularJS $ http发布请求数据和响应并带有未定义的错误

[英]Angularjs $http post request data and response with undefined error

I want to get specific value from server side. 我想从服务器端获取特定值。 Therefore I am using $http to pass variable from front-end (Angularjs javascript) to backend(php). 因此,我正在使用$ http将变量从前端(Angularjs javascript)传递到后端(php)。 After server side (php) get the value from front-end. 服务器端(php)之后从前端获取值。 It will run sql query to get data from mysql and return the data to front-end. 它将运行sql查询以从mysql获取数据,并将数据返回到前端。 However, in my console of front-end, it shows undefined error. 但是,在我的前端控制台中,它显示了未定义的错误。 The following is my code: 以下是我的代码:

Front-end Javascript 前端Javascript

$http({
    method: "post",
    url: "http://localhost/php/UpdateLastLogin.php",
    data: {
        user_name: user_name,
        CurrentTimestamp: CurrentTimestamp
        },
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(response) {
    console.log(response);
}).error(function(response) {
    console.log(response);
});

Backend PHP 后端PHP

<?php
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Credentials: true");
    header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
    header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
    header('P3P: CP="CAO PSA OUR"'); 
    header("Content-Type: application/json; charset=utf-8");


    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    @$CurrentTimestamp = $request->CurrentTimestamp;
    @$user_name = $request->user_name;

    $servername = "localhost";
    $username = "jack";
    $password = "1234";
    $dbname = "daikinchat";

    $CurrentTimestamp = '"' . $CurrentTimestamp . '"';
    //$user_name = '"' . $user_name . '"';
    $user_name = "Lisa Wong";

    $conn = new mysqli($servername, $username, $password, $dbname);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

    $query = "SELECT last_login_timestamp FROM user_directory WHERE username = '$user_name'";
    $result = mysqli_query($conn, $query);
    $row = mysqli_fetch_row($result);

    echo $row[0];

?>

The problem may be in your $http call, my $http calls look like: 问题可能出在您的$ http呼叫中,我的$ http呼叫看起来像:

$http({
            method: 'post',
            url:'http://localhost/php/UpdateLastLogin.php',
            data: {...},
            config: 'Content-Type: application/json;'
        }).then(function (response) {
            console.log(response);
        }, function (response) {
            console.log(response);
        });

If I'm not wrong, you can use both, headers or config, but seems like you aren't using properly the attribute headers, the documentation says: 如果我没记错的话,您可以同时使用标头或配置,但是似乎您没有正确使用属性标头,该文档说:

  • headers – {function([headerName])} – Header getter function. 标头– {function([headerName])} –标头获取函数。
  • config – {Object} – The configuration object that was used to generate the request. config – {Object} –用于生成请求的配置对象。

So try to change your 'Content-Type' to 'application/json', because that's what you are sending in you http request body. 因此,请尝试将“ Content-Type”更改为“ application / json”,因为这就是您在http请求正文中发送的内容。 If it doesn't work, change headers to config to test. 如果不起作用,请将标头更改为config进行测试。

If it doesn't work either, I'd use any tool like, 'postman' to check the API, and make sure that it's working, and try to debug the PHP code (I can't help you with this part) 如果两者都不起作用,我将使用诸如“邮递员”之类的任何工具来检查API,并确保其正常工作,然后尝试调试PHP代码(这一部分我无法为您提供帮助)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM