【问题标题】:Angularjs $http post request data and response with undefined errorAngularjs $http 发布请求数据和未定义错误的响应
【发布时间】:2016-10-10 07:15:28
【问题描述】:

我想从服务器端获得特定的价值。因此,我使用 $http 将变量从前端(Angularjs javascript)传递到后端(php)。在服务器端(php)从前端获取值之后。它将运行 sql 查询以从 mysql 获取数据并将数据返回到前端。但是,在我的前端控制台中,它显示未定义的错误。以下是我的代码:

前端 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);
});

后端 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];

?>

【问题讨论】:

  • 显示哪一行?
  • @Sajeetharan error(function(response) { console.log(response);}
  • 在 $http 请求中尝试内容类型 application/json

标签: javascript php mysql angularjs http-post


【解决方案1】:

问题可能出在您的 $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);
        });

如果我没记错的话,您可以同时使用标头或配置,但似乎您没有正确使用属性标头,文档说:

  • headers – {function([headerName])} – Header getter 函数。
  • config – {Object} – 用于生成请求的配置对象。

所以尝试将您的“Content-Type”更改为“application/json”,因为这就是您在 http 请求正文中发送的内容。如果不起作用,请将标头更改为 config 以进行测试。

如果它也不起作用,我会使用“邮递员”之类的任何工具来检查 API,并确保它正常工作,然后尝试调试 PHP 代码(我无法帮助您)部分)

【讨论】:

  • 我认为主要问题是我将对象发布到后端,但在我的 php 中,回显是一个字符串而不是对象。因此,它出现了 SyntaxError: Unexpected token a in JSON at position 0. 我的响应可以接受字符串吗?
  • 您可以使用 JSON.stringfy(json_object) 获取字符串,并使用 JSON.parse(string_object) 从该字符串中再次获取 json。因此,请确保在使用之前将对象转换为 json 或字符串...
  • 我刚刚注意到,修改你的 $http 调用,使用这个:` $http({ method: "post", url: "localhost/php/UpdateLastLogin.php", data: { user_name: user_name, CurrentTimestamp: CurrentTimestamp },标题:{ 'Content-Type': 'application/json' } }.then(function (response) { console.log(response); }, function (response) { console.log(response); }) ; `
猜你喜欢
  • 2020-01-08
  • 2018-08-19
  • 1970-01-01
  • 2013-03-07
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2015-04-02
  • 2019-08-17
相关资源
最近更新 更多