zoukankan      html  css  js  c++  java
  • Angualr 向后台传参无法接收的问题

    原始代码:

    			$http({
                    method:'POST',
                    url:'/api/rabbit/queue.json',
                    data:{queue:$scope.queueName,msg:$scope.msgValue}
                }).then(function (response) {
                    $scope.msgNum = response.data;
                    console.log(response.data);
                }, function (response) {
                    console.log(response.data);
                });
    

    后台则无法接收参数

    需要修改为

    			$http({
                    method:'POST',
                    url:'/api/rabbit/queue.json',
                    params:{queue:$scope.queueName,msg:$scope.msgValue},
                    paramSerializer: '$httpParamSerializerJQLike'
                }).then(function (response) {
                    $scope.msgNum = response.data;
                    console.log(response.data);
                }, function (response) {
                    console.log(response.data);
                });
    

    这里添加了一个参数:paramSerializer,并且修改data为params。

    官方文档说明:
    Default Transformations
    The $httpProvider provider and $http service expose defaults.transformRequest and defaults.transformResponse properties. If a request does not provide its own transformations then these will be applied.

    You can augment or replace the default transformations by modifying these properties by adding to or replacing the array.

    Angular provides the following default transformations:

    Request transformations ($httpProvider.defaults.transformRequest and $http.defaults.transformRequest):

    If the data property of the request configuration object contains an object, serialize it into JSON format.
    Response transformations ($httpProvider.defaults.transformResponse and $http.defaults.transformResponse):

    If XSRF prefix is detected, strip it (see Security Considerations section below).
    If JSON response is detected, deserialize it using a JSON parser.

    默认情况下,将data属性转换成JSON格式,与JQuery不一致,那么就需要修改转换方式,官方文档提供了两种解决方案:
    Alternative $http params serializer that follows jQuery's param() method logic. The serializer will also sort the params alphabetically.

    To use it for serializing $http request parameters, set it as the paramSerializer property:

    $http({
      url: myUrl,
      method: 'GET',
      params: myParams,
      paramSerializer: '$httpParamSerializerJQLike'
    });
    

    It is also possible to set it as the default paramSerializer in the $httpProvider.

    Additionally, you can inject the serializer and use it explicitly, for example to serialize form data for submission:

    .controller(function($http, $httpParamSerializerJQLike) {

     $http({
        url: myUrl,
        method: 'POST',
        data: $httpParamSerializerJQLike(myData),
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      });
    });
    
  • 相关阅读:
    PAT——1069. 微博转发抽奖
    PAT——1068. 万绿丛中一点红
    PAT——1066. 图像过滤
    PAT——1065. 单身狗
    PAT——1064. 朋友数
    PAT——1063. 计算谱半径
    PAT——1062. 最简分数
    PAT——1061. 判断题
    PAT——1060. 爱丁顿数
    PAT——1059. C语言竞赛
  • 原文地址:https://www.cnblogs.com/bacazy/p/5823744.html
Copyright © 2011-2022 走看看