XMLHttpRequest失败:{statusText":Not Found"," status :: 404," responseURL":https://api.parse.com/1/users"离子+解析

时间:2015-05-24 09:40:10

标签: javascript android parse-platform ionic-framework ionic

我一直试图解决这个问题。我一直致力于离子+解析登录,注册,忘记密码应用程序。据说Ionic是前端而parse.com是数据库。

当从命令行使用Ionic服务时,这些功能完全可以从web broswer中的本地主机工作,但是当我为Android(离子构建android)或模拟(离子模拟android)构建应用程序或在Android设备上测试它时我收到一系列错误消息:

当我尝试登录时;发生了意外错误,请再试一次

当我尝试重置密码时;发生了意外错误,请再试一次

当我尝试注册时,显示此错误消息:

XMLHttpRequest失败:{statusText&#34;:Not Found&#34;,&#34; status :: 404,&#34; responseURL&#34;:https://api.parse.com/1/users&#34;,&#34;响应&#34;:&#34;&#34; resp onseType&#34;:&#34;&#34;。&#34; responseXML&#34;:null,&#34; responseText&#34;:&# 34;&#34;&#34;上传&#34;:{loadend&#34;:空,&#34;的onload&#34;:空,&#34; onprogress&#34;:空,&#34; onloadstart&#34;:空,&#34; onloadend&#34;:空,:的onload&#34;:空,&#34;的onerror&#34;:空&#34; onabort&#34;:空}&#34 ; withCredentials&#34;:假,&#34; readyState的&#34:4&#34;超时&#34;:0,&#34; ontimeout&#34;:空,&#34; onprogress&#34;:空, &#34; onloadstart&#34;:空,:onloadend&#34;:空,&#34;的onload&#34;:空,&#34;的onerror&#34;:空,:onabort&#34;:空} < / p>

这是我的代码:

注册页面:模板文件夹

<ion-view title="Register">
    <ion-content has-header="true" has-tabs="true" padding="true">
        <div class="list">
            <label class="item item-input">
                <input type="email" ng-model="user.email" placeholder="Email">
            </label>
            <label class="item item-input">
                <input type="password" ng-model="user.password" placeholder="Password">
            </label>
            <label class="item item-input">
            <input type="text" ng-model="user.name" placeholder="First Name">
  </label>
            <label class="item item-input item-stacked-label">
                <span class="input-label">Date of Birth</span>
                <input type="date" ng-model="user.dob">
            </label>
        </div>
        <div class="assertive" ng-show="error.message">{{error.message}}</div>
        <button class="button button-block button-stable" ng-click="register()">
            CREATE ACCOUNT
        </button>
        By creating an account you agree to the Terms of Use and Privacy Policy.
    </ion-content>
</ion-view>

controllers.js

angular.module('ionicParseApp.controllers', [])
.controller('AppController', function($scope, $state, $rootScope, $ionicHistory, $stateParams) {
    if ($stateParams.clear) {
        $ionicHistory.clearHistory();
        $ionicHistory.clearCache();
    }
    $scope.logout = function() {
        Parse.User.logOut();
        $rootScope.user = null;
        $rootScope.isLoggedIn = false;
        $state.go('welcome', {
            clear: true
        });
    };
})
.controller('WelcomeController', function($scope, $state, $rootScope, $ionicHistory, $stateParams) {
    if ($stateParams.clear) {
        $ionicHistory.clearHistory();
        $ionicHistory.clearCache();
    }
    $scope.login = function() {
        $state.go('app.login');
    };
    $scope.signUp = function() {
        $state.go('app.register');
    };
    if ($rootScope.isLoggedIn) {
        $state.go('app.home');
    }
})
.controller('HomeController', function($scope, $state, $rootScope) {
    if (!$rootScope.isLoggedIn) {
        $state.go('welcome');
    }
})
.controller('LoginController', function($scope, $state, $rootScope, $ionicLoading) {
    $scope.user = {
        username: null,
        password: null
    };
    $scope.error = {};
    $scope.login = function() {
        $scope.loading = $ionicLoading.show({
            content: 'Logging in',
            animation: 'fade-in',
            showBackdrop: true,
            maxWidth: 200,
            showDelay: 0
        });
        var user = $scope.user;
        Parse.User.logIn(('' + user.username).toLowerCase(), user.password, {
            success: function(user) {
                $ionicLoading.hide();
                $rootScope.user = user;
                $rootScope.isLoggedIn = true;
                $state.go('app.home', {
                    clear: true
                });
            },
            error: function(user, err) {
                $ionicLoading.hide();
                // The login failed. Check error to see why.
                if (err.code === 101) {
                    $scope.error.message = 'Invalid login credentials';
                } else {
                    $scope.error.message = 'An unexpected error has ' +
                        'occurred, please try again.';
                }
                $scope.$apply();
            }
        });
    };
    $scope.forgot = function() {
        $state.go('app.forgot');
    };
})
.controller('ForgotPasswordController', function($scope, $state, $ionicLoading) {
    $scope.user = {};
    $scope.error = {};
    $scope.state = {
        success: false
    };
    $scope.reset = function() {
        $scope.loading = $ionicLoading.show({
            content: 'Sending',
            animation: 'fade-in',
            showBackdrop: true,
            maxWidth: 200,
            showDelay: 0
        });
        Parse.User.requestPasswordReset($scope.user.email, {
            success: function() {
                // TODO: show success
                $ionicLoading.hide();
                $scope.state.success = true;
                $scope.$apply();
            },
            error: function(err) {
                $ionicLoading.hide();
                if (err.code === 125) {
                    $scope.error.message = 'Email address does not exist';
                } else {
                    $scope.error.message = 'An unknown error has occurred, ' +
                        'please try again';
                }
                $scope.$apply();
            }
        });
    };
    $scope.login = function() {
        $state.go('app.login');
    };
})
.controller('RegisterController', function($scope, $state, $ionicLoading, $rootScope) {
    $scope.user = {};
    $scope.error = {};
    $scope.register = function() {
        // TODO: add age verification step
        $scope.loading = $ionicLoading.show({
            content: 'Sending',
            animation: 'fade-in',
            showBackdrop: true,
            maxWidth: 200,
            showDelay: 0
        });
        var user = new Parse.User();
        user.set("username", $scope.user.email);
        user.set("password", $scope.user.password);
        user.set("email", $scope.user.email);
        user.signUp(null, {
            success: function(user) {
                $ionicLoading.hide();
                $rootScope.user = user;
                $rootScope.isLoggedIn = true;
                $state.go('app.home', {
                    clear: true
                });
            },
            error: function(user, error) {
                $ionicLoading.hide();
                if (error.code === 125) {
                    $scope.error.message = 'Please specify a valid email ' +
                        'address';
                } else if (error.code === 202) {
                    $scope.error.message = 'The email address is already ' +
                        'registered';
                } else {
                    $scope.error.message = error.message;
                }
                $scope.$apply();
            }
        });
    };
})
.controller('MainController', function($scope, $state, $rootScope, $stateParams, $ionicHistory) {
    if ($stateParams.clear) {
        $ionicHistory.clearHistory();
    }
    $scope.rightButtons = [{
        type: 'button-positive',
        content: '<i class="icon ion-navicon"></i>',
        tap: function(e) {
            $scope.sideMenuController.toggleRight();
        }
    }];
    $scope.logout = function() {
        Parse.User.logOut();
        $rootScope.user = null;
        $rootScope.isLoggedIn = false;
        $state.go('welcome', {
            clear: true
        });
    };
    $scope.toggleMenu = function() {
        $scope.sideMenuController.toggleRight();
    };
});

apps.js

// setup an abstract state for the tabs directive
            .state('welcome', {
                url: '/welcome?clear',
                templateUrl: 'templates/welcome.html',
                controller: 'WelcomeController'
            })

            .state('app', {
                url: '/app?clear',
                abstract: true,
                templateUrl: 'templates/menu.html',
                controller: 'AppController'
            })

            .state('app.home', {
                url: '/home',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/home.html',
                        controller: 'HomeController'
                    }
                }
            })

            .state('app.login', {
                url: '/login',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/login.html',
                        controller: 'LoginController'
                    }
                }
            })

            .state('app.forgot', {
                url: '/forgot',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/forgotPassword.html',
                        controller: 'ForgotPasswordController'
                    }
                }
            })

            .state('app.register', {
                url: '/register',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/register.html',
                        controller: 'RegisterController'
                    }
                }
            });

        $urlRouterProvider.otherwise('/welcome');
    })
    .run(function ($state, $rootScope) {
        Parse.initialize('**hidden**', '**hidden**');
        var currentUser = Parse.User.current();
        $rootScope.user = null;
        $rootScope.isLoggedIn = false;

        if (currentUser) {
            $rootScope.user = currentUser;
            $rootScope.isLoggedIn = true;
            $state.go('app.home');
        }
    });

1 个答案:

答案 0 :(得分:5)

可能因为您必须添加插件whitelist

cordova plugin add cordova-plugin-whitelist

如果要保存对config.xml文件的引用:

cordova plugin add cordova-plugin-whitelist --save

你应该有这个:

<access origin="*" />
config.xml 文件中的

。如果您使用plugin see的规格,则可以将您的域名或外部域名列入白名单。
像这样离开就意味着你列出了所有要求的白名单:所有请求。

新的cordova版本引入了其中一些功能 更多信息here

如果您想查看究竟是什么原因导致您的应用程序出现问题我建议您通过USB将Android设备插入计算机,激活调试功能并使用Chrome作为检查员在浏览器中访问此chrome://inspect/#devices

您应该可以像使用标准Web应用程序一样查看设备并进行调试。