$ http在AngularJS中返回404错误

时间:2014-09-13 20:51:37

标签: javascript angularjs http cordova http-status-code-404

我在Cordova / PhoneGap上使用AngularJS和Ionic Framework构建的移动应用程序出错。我已经向一些用户发布了一个用于测试的版本,只有少数用户告诉我他们的用户名和密码没有进行身份验证。我无法复制错误,因为它似乎只与这些少数人有问题。

我的代码如下。它连接到我们的一台服务器并检查用户名和密码。如果username / pass有效,它将返回“1”,如果JSONP格式无效,则返回“0”。如果请求失败,那么$ http请求有一个错误处理程序,它将打印出错误代码。我添加了一个警报,弹出$ http错误的状态,用户告诉我他们有404错误。具有1.3.0和1.2.17的AngularJS具有相同的问题。

我很困惑为什么有些用户可能会遇到问题。我怀疑它可能是CORS,但我使用的是JSONP,并且大多数人都在进行身份验证而没有任何问题。我的config.xml也设置为<access origin="*" />

$scope.attemptLogin = function() {
  var reqUrl = "https://MYSERVER.com/auth.php?";

  //If input has email, strip it out
  var newUser = ($scope.credentials.username).toLowerCase();
  var domainMatch = newUser.match(/@MYSERVER.com/g);
  $scope.credentials.username = newUser.replace(domainMatch, '');

  //Authenticate the username and password
  $http({
    method: 'JSONP',
    url: reqUrl + "other_auth_code_stuff" + "&callback=JSON_CALLBACK"

  }).success(function(data, status) {

    //Username and password valid
    if (data == 1) {

          //User is authenticated
      AuthService.setUserAuthenticated(true);
      console.log("Login successful");

      //Redirect the state since we're successful
      $state.go('app.dashboard');
    }
    //Username and password invalid
    else {

        //User is not authenticated
        AuthService.setUserAuthenticated(false);
        alertPass(); //Popup to tell user that user/pw is wrong
        console.log("Username/Password incorrect");
    }

  }).error(function(data, status) {
     AuthService.setUserAuthenticated(false);

      alert(status); //RETURNING 404
      alertError();
      console.log("Authentication failure");
  });           

};

0 个答案:

没有答案