http get返回错误未定义

时间:2015-12-30 10:11:06

标签: javascript angularjs

我尝试使用http获取后端函数来检索数据,但是当我运行javascript http触发器错误而错误未定义时。我不明白出了什么问题,因为我没有遇到任何错误。

HTML

<body ng-app="postApp" ng-controller="postController">
    <div id="container">
        <div class="tab">
            <div>
                <p ng-show="errors">{{errors}}</p>
                <p ng-show="message">Status: {{message}}</p>
                <form ng-submit="startJobs()">
                    <label ng-repeat="job in jobs">
                        <input
                                type="checkbox"
                                name="selectedJobs[]"
                                value="{{job.name}}"
                                ng-model="job.selected"
                        > {{job.name}}
                    </label>
                    <p/>
                    <input type="submit" id="startJobs" value="Start jobs" />
                </form>
            </div>

            <p ng-show="message">Status: {{status}}</p>
            <div>
                <form ng-submit="getJobStatus()">
                    <input type="submit" id="jobStatus" value="Progress" />
                </form>
            </div>
        </div>
    </div>
</body>

的javascript

<script>
    var postApp = angular.module('postApp', []);
    postApp.controller('postController', function($scope, $http, filterFilter) {

        $scope.jobs = [
            { name: 'Response status', value: 'responseStatus', selected: true },
            { name: 'test', value: 'test', selected: false }
        ];

        $scope.selection = [];

        $scope.selectedJobs = function selectedJobs() {
            return filterFilter($scope.jobs, { selected: true });
        };

        $scope.$watch('jobs|filter:{selected:true}', function (nv) {
            $scope.selection = nv.map(function (jobs) {
                return jobs.name;
            });
        }, true);

        $scope.checkedJobs = [];

        $scope.user = {};
        $scope.startJobs = function() {
            angular.forEach($scope.jobs, function(value, key) {
                if(value.selected == true) {
                    $scope.checkedJobs.push(value.value);
                }
            });
            $http({
                method  : 'GET',
                url     : 'api/start_analyze/' + $scope.checkedJobs + '/',
                data    : $scope.user,
                headers : {'Content-Type': 'application/x-www-form-urlencoded'}
            }).success(function(data) {
                $scope.message = data;
            }).error(function(error){
                $scope.error = error; //here i get undefined on error.
            });
        };
        $scope.getJobStatus = function() {
            $http({
                method  : 'GET',
                url     : 'api/get_analyze_status/',
                data    : $scope.user,
                headers : {'Content-Type': 'application/x-www-form-urlencoded'}
            }).success(function(data) {
                $scope.status = data;
            }).error(function(error){
                $scope.error = error; //here i get undefined on error.
            });
        };
    });
</script>

编辑:来自控制台的错误

  

错误:JSON.parse:JSON第1行第1列的意外字符   数据   fromJson @ http://localhost:8081/bower_components/angular/angular.js:1271:9   defaultHttpResponseTransform @ http://localhost:8081/bower_components/angular/angular.js:9460:1   transformData /&LT; @ http://localhost:8081/bower_components/angular/angular.js:9551:12   的forEach @ http://localhost:8081/bower_components/angular/angular.js:340:11   transformData @ http://localhost:8081/bower_components/angular/angular.js:9550:3   transformResponse @ http://localhost:8081/bower_components/angular/angular.js:10319:21   processQueue @ http://localhost:8081/bower_components/angular/angular.js:14792:28   scheduleProcessQueue /&LT; @ http://localhost:8081/bower_components/angular/angular.js:14808:27   $ RootScopeProvider / $这gethttp://本地主机:8081 / bower_components /角度/ angular.js:16052:16   $ RootScopeProvider / $这gethttp://本地主机:8081 / bower_components /角度/ angular.js:15870:15   $ RootScopeProvider / $这gethttp://本地主机:8081 / bower_components /角度/ angular.js:16160:13   做@ http://localhost:8081/bower_components/angular/angular.js:10589:36   completeRequest @ http://localhost:8081/bower_components/angular/angular.js:10787:7   requestLoaded @ http://localhost:8081/bower_components/angular/angular.js:10728:1

     

http://localhost:8081/bower_components/angular/angular.js第12520行

来自服务器端的响应是。

  

status = 200 response =&#34;开始工作&#34; headersString =   &#34;内容类型:应用... tty(8.1.16.v20140903)\ r \ n&#34; statusText =   &#34; OK&#34;

0 个答案:

没有答案