javascript - 使用 Resources 制作不带引号的 JSON

标签 javascript json

您好,我在将 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/

相关文章:

javascript - 右侧的 Bootstrap 静态设置面板

javascript - jQuery .offset() 在每个浏览器中根本无法正常工作

javascript - 如何使用复杂的数据结构来填充 JavaScript 和 jQuery 中的 div 或表格

javascript - 在 Django 中,我知道如何构建模板。渲染到响应 ('something.html' )。但是,如何在其中包含 JSON 对象呢?

c - 使用 ‏μC/OS-II 直接解析 json

json - AFNetworking 和 Swift - 保存 json 响应

javascript - 删除 <br> 并仅保留文本后加倍中断

javascript - 双亲在 Jade 中不起作用

javascript - NG-keypress 防止特定 charCode 的默认值

java - Json 的自定义反/序列化涉及某些接口(interface)的多个实现