• 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'
        }
      });
    });
    
  • 相关阅读:
    排序
    FileOutputStream文件写入 —— 覆盖、追加
    判断一个地图坐标是否在中国镜内
    地图坐标转换
    全文检索 使用最新lucene3.0.3+最新盘古分词 pangu2.4 .net 实例
    blob转base64位 base64位转blob
    div中粘贴图片并上传服务器 div中拖拽图片文件并上传服务器
    DIV 粘贴插入文本或者其他元素后,移动光标到最新处
    project 计划添加编号或 任务分解时为任务添加编号
    修改project任务的默认开始时间
  • 原文地址:https://www.cnblogs.com/bacazy/p/5823744.html
Copyright © 2020-2023  润新知