Javascript $http.post()到达服务器,但在.success中不发出任何警报
我对角度代码有点问题。我使用$http.post将数据发送到php文件。该php文件将数据保存到数据库中。您可以在下面看到我的控制器:Javascript $http.post()到达服务器,但在.success中不发出任何警报,javascript,php,angularjs,Javascript,Php,Angularjs,我对角度代码有点问题。我使用$http.post将数据发送到php文件。该php文件将数据保存到数据库中。您可以在下面看到我的控制器: app.controller('send_data', function($scope, $http) { $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; $scope.data1 = "hello"
app.controller('send_data', function($scope, $http) {
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
$scope.data1 = "hello";
$scope.data2 = "world";
$scope.test_func = function() {
data = {
'data1' : $scope.data1,
'data2' : $scope.data2
};
$http.post('server/file.php', data)
.success(function()
{
alert("Success");
console.log('success');
})
.error(function()
{
console.log('error');
});
}
}))
我使用ng click从按钮调用test_func。我可以看到数据已保存到数据库中,但没有显示带有消息“Success”的弹出窗口。发生了什么事
提前谢谢大家,
John最后我发现了问题,那就是服务器端忘记了“echo‘message’”。移除它,现在一切都好了 谢谢大家的帮助 因此,上面的代码正在运行,您还可以看到下面的php代码:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
function mysql_con()
{
$servername = "localhost";
$username = "user";
$password = "admin";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
return $conn;
}
$conn = mysql_con();
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$data1 = $request->data1;
$data2 = $request->data2;
$sql = "INSERT INTO ext_db.test (data1, data2) VALUES ('".$data1."', '".$data2."')";
if ($conn->query($sql) === TRUE) {
//echo "New record created successfully";
$data = array('success' => true);
echo json_encode($data);
} else {
//echo "Error: " . $sql . "<br>" . $conn->error;
$data = array('success' => false);
echo json_encode($data);
}
$conn->close();
?>
您获得了哪个http状态码?请在浏览器网络控制台中查看。尝试在post和success行之间添加.then(函数(响应){console.log(响应)}),以检查正在发生的情况。success/error
已被弃用,但仍应正常工作。这表明服务器正在响应错误消息。错误函数说什么?您的开发工具在控制台和网络(工作)选项卡中说了什么?Console.log(status+'-'+data);它被触发了吗?