使用ngResources制作没有qutotes的JSON

时间:2016-07-11 09:27:04

标签: javascript json

您好我将JSON发布到我的rest API时遇到问题。 ngResource正在传递带有qutes的字符串,它看起来像

  

{“name”:“Jan”,“surname”:“Kowalski”,“position”:“{”positionid“:0},”id“:1}”

但我需要这样

  

{“name”:“Jan”,“surname”:“Kowalski”,“position”:{“positionid”:0},“id”:1}

这是我在jsp中的代码:

controller('addNewWorkerController',    
['$scope','formService','positionlist',function($scope,formService,positionlist) {

 $scope.saveData=function () {

 var str=  "{positionID:"+$scope.formInfo.position+"}";
        $scope.position = str;
          window.alert(str);
        console.log($scope.formInfo);
        formService.save($scope.formInfo);
    }

我的formService代码

 service.factory('formService',['$resource',function($resource){ 

 return $resource('http://localhost:8080/workers',{},{
  save:{
    method:'POST'
  }  
}
)


 }]);

1 个答案:

答案 0 :(得分:1)

我想你需要这样做:

$scope.saveData=function () {
    var str =  {positionID : $scope.formInfo.position }; // <---instead of js object string convert it to js object.
    $scope.position = str;
    window.alert(str);
    console.log($scope.formInfo);
    formService.save($scope.formInfo);
}