【问题标题】:XMLHttpRequest failed: {statusText":Not Found","status::404,"responseURL":https://api.parse.com/1/users" Ionic + ParseXMLHttpRequest 失败:{statusText":Not Found","status::404,"responseURL":https://api.parse.com/1/users" Ionic + Parse
【发布时间】:2015-08-05 23:22:04
【问题描述】:

我一直在努力解决这个问题。我一直在研究离子+解析登录、注册、忘记密码的应用程序。话虽如此,Ionic 是前端,parse.com 是数据库。

从命令行使用 Ionic 服务时,这些功能可以在网络浏览器的本地主机上完美运行,但是当我为 android (ionic build android) 或 emulate (ionic emulate android) 构建应用程序或在 android 设备上对其进行测试时我收到一系列错误消息:

当我尝试登录时;出现意外错误,请重试

当我尝试重设密码时;出现意外错误,请重试

当我尝试注册时,我会看到以下错误消息:

XMLHttpRequest 失败:{statusText":Not Found","status::404,"responseURL":https://api.parse.com/1/users","re​​sponse":""responseType":""."responseXML":null,"responseText ":"","upload":{loadend":null,"onload":null,"onprogress":null,"onloadstart":null,"onloadend":null,:onload":null,"onerror":null "onabort":null},"withCredentials":false,"readyState":4"timeout":0,"ontimeout":null,"onprogress":null,"onloadstart":null,:onloadend":null,"onload ":null,"onerror":null,:onabort":null}

这是我的代码:

注册页面:模板文件夹

<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');
        }
    });

【问题讨论】:

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


    【解决方案1】:

    可能是因为您必须添加插件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 应用程序一样对其进行调试。

    【讨论】:

    • 我从来没有在使用 parse 的 ionic 应用程序中使用白名单插件,这可能会起作用......但我不确定它是否需要。
    • 谢谢,成功了!终于可以和外界交流了。
    • @user2993476:您应该接受答案,而不是用“谢谢”来回答您自己的问题。干杯。
    • 我支持这个答案。在我更新 ionic/cordova 之前,我的应用程序运行良好。这个建议解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多