在angularjs php中显示错误验证消息
在后端,我检查以确保php变量userid不是空的,并且name变量是相同的,否则让它返回错误消息。我的前视图是angularjs 问题在于我的数据验证检查没有返回错误消息。 我已经在angularjs中实现了验证检查在angularjs php中显示错误验证消息,php,angularjs,Php,Angularjs,在后端,我检查以确保php变量userid不是空的,并且name变量是相同的,否则让它返回错误消息。我的前视图是angularjs 问题在于我的数据验证检查没有返回错误消息。 我已经在angularjs中实现了验证检查 if (response.data.trim() === 'data_empty') { //show error msg $scope.errorMsg = true; //hide the msg after 5 secs
if (response.data.trim() === 'data_empty') {
//show error msg
$scope.errorMsg = true;
//hide the msg after 5 secs
$timeout(function(){
$scope.errorMsg = false;
//window.location = 'errorpage.php';
}, 5000);
}
if (response.data.trim() === 'name_not_the_same') {
//show error msg
$scope.errorMsg2 = true;
//hide the msg after 5 secs
$timeout(function(){
$scope.errorMsg2 = false;
}, 5000);
}
在html上,我有
<div class="alert alert-danger" ng-show="errorMsg">
<strong>Wrong!</strong> All Field Must be Filled.!.
</div>
<div class="alert alert-danger" ng-show="errorMsg2">
<strong>Wrong!</strong> Name must be the same.!.
</div>
错了必须填写所有字段。!。
错了名称必须相同。!。
如何在angularjs中验证数据并返回消息。下面是完整的代码
<!doctype html>
<html>
<head>
<title></title>
<link href="style.css" type="text/css" rel="stylesheet" />
<style>
.comment{
border-bottom: 1px solid gray;
padding: 5px;
}
</style>
</head>
<body ng-app='myapp'>
<div class="content" ng-controller='fetchCtrl' >
<div class="post" ng-repeat='post in posts'>
<h1 >{{ post.title }}</h1>
<div class="post-text">
{{ post.content }}
</div>
</div>
</div>
<div class="alert alert-danger" ng-show="errorMsg">
<strong>Wrong!</strong> All Field Must be Filled.!.
</div>
<div class="alert alert-danger" ng-show="errorMsg2">
<strong>Wrong!</strong> Name must be the same.!.
</div>
<!-- Script -->
<script src="angular.min.js"></script>
<script>
var fetch = angular.module('myapp', []);
fetch.controller('fetchCtrl', ['$scope', '$http','$timeout', function ($scope, $http, $timeout) {
// Fetch post data
$scope.getPosts = function(){
$http({
method: 'post',
url: 'likeunlike.php',
data: {request: 1}
}).then(function successCallback(response) {
$scope.posts = response.data;
if (response.data.trim() === 'data_empty') {
//show error msg
$scope.errorMsg = true;
//hide the msg after 5 secs
$timeout(function(){
$scope.errorMsg = false;
//window.location = 'errorpage.php';
}, 5000);
}
if (response.data.trim() === 'name_not_the_same') {
//show error msg
$scope.errorMsg2 = true;
//hide the msg after 5 secs
$timeout(function(){
$scope.errorMsg2 = false;
}, 5000);
}
});
}
$scope.getPosts(); // Fetch post data
}
]);
</script>
</body>
</html>
.评论{
边框底部:1px纯色灰色;
填充物:5px;
}
{{post.title}}
{{post.content}}
错了必须填写所有字段。!。
错了名称必须相同。!。
var fetch=angular.module('myapp',[]);
controller('fetchCtrl',['$scope','$http','$timeout',函数($scope,$http,$timeout){
//获取post数据
$scope.getPosts=函数(){
$http({
方法:“post”,
url:'likeunlike.php',
数据:{请求:1}
}).then(函数成功回调(响应){
$scope.posts=response.data;
if(response.data.trim()=='data\u empty'){
//显示错误消息
$scope.errorMsg=true;
//5秒后隐藏味精
$timeout(函数(){
$scope.errorMsg=false;
//window.location='errorpage.php';
}, 5000);
}
if(response.data.trim()=='name\u not\u same'){
//显示错误消息
$scope.errorMsg2=true;
//5秒后隐藏味精
$timeout(函数(){
$scope.errorMsg2=false;
}, 5000);
}
});
}
$scope.getPosts();//获取post数据
}
]);
php
<?php
include "config.php";
$data = json_decode(file_get_contents("php://input"));
$request = $data->request;
$userid = '';
$name ='tony';
if($userid !=''){
$response ='data_empty';
echo $response;
exit();
}
if($name !='tony'){
$response1 ='name_not_the_same';
echo $response1;
exit();
}
// Get all posts list and like unlike
if($request == 1){
$response_arr = array();
$query = "SELECT * FROM posts";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$postid = $row['id'];
$title = $row['title'];
$content = $row['content'];
$type = -1;
$response_arr[] = array("id" => $postid, "title" => $title);
}
echo json_encode($response_arr);
exit;
}
看起来您需要先验证JSON解析是否有效:
$data = json_decode(file_get_contents("php://input"));
if (json_last_error() !== JSON_ERROR_NONE) {
echo 'bad_json';
exit();
}
// now safe to use $data
$request = $data->request;
...
问题不在于变量应该作为请求发送还是硬编码发送。我已经尝试了这两种方法,并且它不会通过angularjsSo打印任何错误。因此,在javascript控制台中查看,likeunlike.php
返回什么?angular.min.js:124错误:[ngRepeat:duppes]在angular.min.js:7在angular.min.js:315在angular.min.js:147在m.$digest(angular.min.js:148)在m.$apply(angular.min.js:151)在l(angular.min.js:103)在s(angular.min.js:108)在XMLHttpRequest.y.onload(angular.min.js:109)时,您使用的是Chrome吗?单击网络选项卡并选择likeunlike.php
请求。响应是什么?注意:在第8行的C:\xampp\htdocs\angle\comment\likeunlike.php中尝试获取非对象的属性注意:在第9行的C:\xampp\htdocs\angle\comment\likeunlike.php中尝试获取非对象的属性name\u不相同\u