• 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'
        }
      });
    });
    
  • 相关阅读:
    ThinkPHP-5.0.23新的RCE漏洞测试和POC
    利用DNS日志进行MySQL盲注
    Prometheus自动发现
    relabel_config
    Maven安装
    Redis安装
    Node.js安装
    MySQL-5.7安装
    设计模式【1】------>单例模式
    Java设计模式概念以及六大设计原则
  • 原文地址:https://www.cnblogs.com/bacazy/p/5823744.html
Copyright © 2020-2023  润新知