java - 适当的 json 到后端 post 调用

标签 java json angularjs http post

我在处理 POST rest 调用的不同后端时遇到了一些麻烦。我有两个不同的对象,它们在我的后端通过两种不同的 POST 方法更新。我将对象捕获为 JsonNode,为了解析我需要更新的属性,我创建了一个迭代器,如下所示:

final Iterator<String> fieldNames = attributes.fieldNames();

当我从 Angular 发送数据时出现问题,在一种情况下我需要像 angular.toJson(data) 一样显式发送它以便正确获取所有字段名称,而在另一种情况下我只发送数据(没有 Angular json 转换)。为什么会出现这种行为?这与我创建 $http post 调用的方式有关吗?以下是来自 Angular 的两个不同调用:

$http.post(URL, angular.toJson(data)).success(function(data){
 /*whatever*/ }).error(function(data) {
 /*whatever*/ });

//Second call looks like this
var promise = $http({method: 'POST', url:URL, data:data, cache:'false'});
//this one i resolve using $q.all

我将代码截断为重要的内容。我的数据目前是这样创建的(尝试了多种方法以跳过对 toJson 的需要):

var data = "{\"Attribute1:\"+"\""+$scope.value1+"\","+
             "\"Attribute2:\"+"\""+$scope.value2+"\"}";

我需要如何发送 json 数据才能在我的后端将其正确转换为 JsonNode,以便我可以正确地迭代 fieldNames?

最佳答案

我确实设法找到了一个通用的解决方案,该解决方案在我的后端正确使用了 json。我这样声明我的 json 对象:

$scope.dataToSend = {
    "SomeAttribute" : "",
    "SomeOtherAttribute" : ""
};

然后像这样添加我的值:

$scope.dataTosend.SomeAttribute = someValue;
$scope.dataTosend.SomeOtherAttribute = someOtherValue;

不再需要使用 angular.toJson() 发送数据。

关于java - 适当的 json 到后端 post 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32265170/

相关文章:

java - 如何将 5 行数据作为 1 个元素存储在 ArrayList 中?

json - 在cloudformation中添加lambda条件?

javascript - 幻灯片中 HTML 页面的 AngularJS 路由?

javascript - 无法从 AngularJS 中的 Controller 初始化对象

javascript - 如何强制 Restangular 的 getList 与 Laravel 5 的分页对象一起使用?

java - 如何配置 JDO 事务来模拟在 App Engine Java 中创建数据库序列?

java - jMonkeyEngine 在网格内应用 Material ?

java - 'model' 和 'view' 之间的接口(interface)

ios - 无法将 JSON 值传输到 SecondViewController 中的标签

iOS JSON 解析