您好,我在将 JSON 发布到我的其余 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'
}
}
)
}]);
最佳答案
我想你需要这样做:
$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);
}
关于javascript - 使用 Resources 制作不带引号的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38303458/