AngularJS:如何将数据发送到服务

时间:2017-05-02 09:14:22

标签: javascript java angularjs service

在以下代码中,我成功获得$scope.hero.rating中的评分,并在$scope.hero.order中订购,现在我想将这两个作为Hero对象传递给OrderService.ratingHero这是在OrderService类中定义的,当我这样做时

OrderService.ratingHero($scope.hero).then(function(response)

它给我一个错误500 ...有人帮我解决这个问题吗?

'use strict';
angular.module('Orders').controller('RatingHeroCtrl',['$scope','$state', '$ionicModal', 'MessageService', 'SettingService', 'OrderService','UserService',

  function($scope, $state, $ionicModal, MessageService, SettingService,OrderService,UserService) {
    $scope.heroName = "Danish";
    (function initialize(){
        $scope.hero = {};
    $scope.rider = {
      ratingsObject : {
                        iconOn: 'ion-ios-star', //Optional
                        iconOff: 'ion-ios-star-outline', //Optional
                        iconOnColor: 'rgb(200, 200, 100)', //Optional
                        iconOffColor: 'rgb(200, 100, 100)', //Optional
                        rating: 0, //Optional
                        minRating: 0, //Optional
                        // readOnly: ratingReadOnly, //Optional
                        callback: function(rating, index) { //Mandatory    

                            $scope.ratingsCallback(rating,index);
                        }
                    }
        }
    })()
$scope.ratingsCallback = function(rating, index) {

  $scope.hero.rating = rating;

OrderService.getOrder($state.params.orderId,
            function(response) {
                $scope.hero.order = response;
            console.log("order"+$scope.hero.order);
            },
            function(error){
                console.log("error");


            }
            )
    }]);

OrderService.js

angular.module('Orders')
    .service('OrderService', ['$http', '$state', '$resource', '$q', 'SettingService', '$localStorage', "MessageService",
     function($http, $state, $resource, $q, SettingService, $localStorage, MessageService) {
        var orderResource = $resource(SettingService.baseUrl + "api/orders/:id", {id:'@id'}, {'query':{method:'GET', isArray:false}, 'update':{method:'PATCH'}});
        var service = {
 ratingHero : function(hero){
                return $http({
                    method: "POST",
                    url: SettingService.baseUrl + "api/heroRatings",
                    data: hero
                });
            },

getOrder : function(OrderId, successCallback, failureCallback){
                orderResource.query({id:OrderId}, successCallback, failureCallback);
            }
        }
        return service;
    }]);

2 个答案:

答案 0 :(得分:1)

错误500表示您的服务器端出现错误:

  

500 Internal Server Error
  一个通用错误消息,在遇到意外情况时给出,并且没有更合适的消息。

由于这是一个非常通用的消息,可能有几个可能的问题,但这个问题并不是来自你的AngularJS代码。

我建议您检查是否在Angular中正确拼写了服务器名称/ API,并查看服务器日志,寻找异常/崩溃/错误。

答案 1 :(得分:0)

在调用后续服务之前,使用data[np.equal(digitized, [index_latitide, index_longitude]).all(axis=1)[:, 2]等待这两项:

$q.all

这样,POST服务在从先前的查询返回到服务器之后执行。