AngularJS $http.post 响应确定错误

AngularJS $http.post response determine ERROR

我正在使用 AngularJS $http.post 在我的登录函数中调用 PHP。 PHP 返回一个标记,如果不存在则返回单词 "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();
    });

当我取回正确的令牌时,它可以正常工作。当我找回 "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-encoding/decoding 问题吗? 尝试了所有解决此问题但没有成功。 谢谢。

默认 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
}
  });

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