【发布时间】: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