【问题标题】:calling a web service with multiple data using angular js使用 Angular js 调用具有多个数据的 Web 服务
【发布时间】:2015-02-26 14:28:12
【问题描述】:

我是 angular js 的新手,我想调用在他的请求正文中使用多个数据的 web 服务(用 Spring REST 实现),但我现在不知道如何进行解析。有人能帮助我吗 ?

这是我在控制器中的 Spring 方法:

@RequestMapping(value = "/addpost", method = RequestMethod.POST, headers = "Accept=application/json")
    public @ResponseBody String addpost(@RequestBody PostDto post, @RequestBody UserDto user) {
        postservice.addPost(post, user);
        return "post inserted";

    }

这就是我尝试调用此方法的方式

$scope.addPost=function(x,y){

        $http.post('http://localhost:8080/wall/addpost',[x,y]).
          success(function(data, status, headers, config) {
              $scope.persons.push(data);
             $scope.user="";
             $scope.post="";
          }).
          error(function(data, status, headers, config) {
              alert("erreur");

          })

【问题讨论】:

    标签: javascript angularjs spring spring-mvc


    【解决方案1】:

    如果您查看$http API,您会发现$http.post 第二个参数包含我们要发布到服务器的对象的json。这样就可以{post: x, user: y} & 然后JSON.stringify() 那个json & 将它传递给服务器。您的服务器端方法将无法理解发送数据中的数组。

    代码

    $scope.addPost = function(x, y) {
    
      $http.post('http://localhost:8080/wall/addpost', {
        post: x, //assuming x is post
        user: y //assuming y is use r
      }, headers: {
                 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                 }).
      success(function(data, status, headers, config) {
        $scope.persons.push(data);
        $scope.user = "";
        $scope.post = "";
      }).
      error(function(data, status, headers, config) {
        alert("erreur");
      })
    }
    

    希望对您有所帮助,谢谢。

    【讨论】:

    • 从 Json 字符串化的对象到两个对象 post 和 users 的解析呢? jakson 解析器可能会对其不稳定?
    • @user3765224 有类似的东西,然后我们可以做。我不知道 Jakson 解析器,因为我来自 .Net 背景
    • 加载资源失败:服务器响应状态为400
    • 尝试不使用JSON.stringify()
    • 你的第一部分代码看起来像这样$http.post('http://localhost:8080/wall/addpost', { post: x, //assuming x is post user: y //assuming y is use r }).
    【解决方案2】:

    您需要使用 application/x-www-form-urlencoded 跨数据发送。

    $http({
        method: 'POST',
        url: 'http://localhost:8080/wall/addpost',
        data: $.param({post: x, user: y}),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    })
    

    更多详情请参阅此答案How can I post data as form data instead of a request payload?

    你的函数定义也应该是

    @RequestMapping(value = "/addpost", method = RequestMethod.POST, headers = "Accept=application/json")
        public @ResponseBody String addpost(@RequestParam PostDto post, @RequestParam UserDto user) {
            postservice.addPost(post, user);
            return "post inserted";
    
        }
    

    【讨论】:

    • 我该怎么做? @thedoctor
    • $ 没有定义我应该注入它吗?
    • 能不能在加载angularjs之前加载完整版的jquery,$.param来自jquery。
    • 我下载了它 $.param 现在运行良好但仍然存在问题 HTTP 代码错误 415 类型不受服务器支持
    • 查看服务器日志以查看是否存在异常,另请参阅此答案以查看它是否相关stackoverflow.com/questions/21344352/…。你也可以尝试在你的java代码中去掉注释的标题部分,因为你没有以JSON格式发送整个请求。
    【解决方案3】:

    AngularJS 处理 POST's 的方式与您可能习惯的方式不同(例如 jQuery)。我要做的一件事是为应用程序的.config() 设置默认方式来处理 $http 请求,将标头默认为application/x-www-form-urlencoded 并在发出请求之前序列化对象。

    angular.module('yourApp', [])
    
        .config(['$httpProvider', function ($httpProvider) {
    
            // default header
            $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    
            // using transformRequest this gets handled before each $http execution
    
            $httpProvider.defaults.transformRequest = function (data) {
                if (data === undefined) {
                    return data;
                }
    
                return angular.isObject(data) && String(data) !== '[object File]' 
                    ? $.param(data) // serialize it if it's an object
                    : data;         // otherwise leave it alone
            };
        });
    

    现在您可以像往常一样使用它了:

    $http.post('url', { /* your data in an object */ });
    

    【讨论】:

    • 编译这条语句$.param(data)时出错:data;
    • 您是否将数据作为对象传递?
    • 我将数据填充为 $http.post('localhost:8080/wall/addpost',{post: x, user: y})
    • 为什么你的网址后面有分号?
    • 您实际上可能必须在您的项目中包含 jQuery,不确定 $.param 是否是 angularJS 的 jQuery-lite 的一部分
    猜你喜欢
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多