php - AngularJS $http.post 响应确定错误

标签 php angularjs http httpresponse

我正在使用 AngularJS $http.post 在我的登录函数中调用 PHP。 PHP 返回一个 token ,如果不存在,则返回“ERROR”一词。
PHP代码:

....
echo json_encode($token);
} else {
echo "ERROR";
} 


Controller .js:

   var request = $http({
   method: "post",
   url: constantService.url + 'login.php',
   data,
   headers: {
       'Content-Type': 'application/x-www-form-urlencoded'
   }
   });

 request.success(function(response) {

   $localstorage.set("token", JSON.stringify(response));

   var showAlert = function() {
       var alertPopup = $ionicPopup.alert({
           title: ' successful token-based login',
           template: response
       });
       alertPopup.then(function(res) {
           console.log(res);
           $state.go('home');
       });
   };
   showAlert();

  });

 request.error(function(data, status, headers, config) {

   console.log('An error occurred ');

   var showAlert = function() {
       var alertPopup = $ionicPopup.alert({
           title: 'Error',
           template: 'check your login credentials'
         });
         alertPopup.then(function(res) {
         });
      };
      showAlert();
    });

当我取回正确的 token 时,它可以正常工作。当我返回“ERROR”(不存在标记)这个词时,我在 chrome 检查器中收到以下错误:

**SyntaxError: Unexpected token E**
at Object.parse (native)
at fromJson       (http://localhost:8100/lib/ionic/js/ionic.bundle.js:9764:14)
at defaultHttpResponseTransform (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17278:16)
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:17363:12
at forEach (http://localhost:8100/lib/ionic/js/ionic.bundle.js:9022:20)
at transformData (http://localhost:8100/lib/ionic/js/ionic.bundle.js:17362:3)
at transformResponse (http://localhost:8100/lib/ionic/js/ionic.bundle.js:18088:23)
at processQueue (http://localhost:8100/lib/ionic/js/ionic.bundle.js:21888:27)
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:21904:27
at Scope.$eval (http://localhost:8100/lib/ionic/js/ionic.bundle.js:23100:28)

返回“ERROR”这个词并在我的系统中处理它的正确方法是什么 response.error 功能?这是一个 JSON 编码/解码问题吗? 尝试了所有解决此问题但没有成功。 谢谢。

最佳答案

默认情况下,angular content-typeapplication/json。 我认为您对 header 的覆盖不起作用,可能是因为您发送的 header 之前有数据。

   var request = $http({
   method: "post",
   url: constantService.url + 'login.php',
   data : data, // here key : value pair is missing
   headers: {
       'Content-Type': 'application/x-www-form-urlencoded'
   }
   });

所以 angular 认为你提供了一个 json 响应并将其解析为 json。所以当你的响应是 ERROR 只是试图解析为 json 并抛出解析错误。

以及关于回调。在您发送错误代码或浏览器发现错误之前,错误功能不会被触发。这是错误代码 link

在您的情况下,ERRORjson_encode(token) 都会触发成功函数。所以你应该在成功函数中处理也作为错误函数处理。 你可能会在你的 php 文件中做

if(/*success condition */){
    $json = array('status'=>'success','token'=>$token);
   }else{
    $json = array('status'=>'error','reason'=>'failed to generate token or somthing');
   }
echo json_encode($json);

在你的成功函数中

request.success(function(response) {
  if(response.status === 'success' ){
   $localstorage.set("token", JSON.stringify(response));

   var showAlert = function() {
       var alertPopup = $ionicPopup.alert({
           title: ' successful token-based login',
           template: response
       });
       alertPopup.then(function(res) {
           console.log(res);
           $state.go('home');
       });
   };
   showAlert();
}else{
   console.log(response.message);//you can do as you error function
}
  });

如果不需要,请不要忘记删除 header 设置。 或者,如果您需要它,请在成功函数顶部执行 JSON.parse(response)

关于php - AngularJS $http.post 响应确定错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34837276/

相关文章:

PHP - 上传 utf-8 文件名

javascript - 在 laravel 中使用 Ajax 自动完成表单

javascript - 为选择元素angularJS设置选定值

javascript - ui-router 嵌套 View 和传递变量

android - 在 Android WebView 中获取 HTTP 状态码

http - 无法访问 EC2 (AWS) 上的端口 8080

php - 如何在php中检查文件是否为视频类型?

angularjs - Spring Security REST 和 Angular

html - 使用 Go 服务器时 MIme 类型不正确

php - 为什么使用 google library 创建的辅助日历不会出现在我的日历下?