AngularJS $http.post 响应确定错误


AngularJS $http.post response determine ERROR

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

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


控制器.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();
    });

当我得到一个正确的令牌时,它可以正常工作。当我返回单词"错误"(不存在令牌)时,我在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)
在我的

单词中返回"错误"一词的正确方法是什么response.error-function ?这是 JSON 编码/解码问题吗?尽一切努力解决这个问题,但没有成功。感谢。

默认情况下,

角度content-type application/json 。我认为您对标头的覆盖不起作用,可能是因为您发送的标头之前的数据。

   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'
   }
   });
因此,当您的

响应ERROR时,您只是尝试解析为 JSON 并抛出解析错误。

以及关于回调。 在您发送错误代码或浏览器显示错误之前,错误函数不会触发。 这是错误代码链接

在您的情况下,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
}
  });

如果不需要,请不要忘记删除标题设置。或者,如果您需要它,请在成功功能之上做一个JSON.parse(response)