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'
        }
      });
    });
    
  • 相关阅读:
    Java中的equals方法和==的区别
    C语言学习笔记--单向链表Markdown版本
    Tamias Blog's
    nginx图片服务器
    Nginx安装解决报错
    Detour框架注入样本无法正常启动(0x000007b)
    结构体指针中包含结构体指针
    Linux开发准备:Ubuntu14.04+Samba+MobaXterm+Source Insight 4.0
    在Visual Studio中将dll以资源的形式嵌入exe中
    MACD的价值不在于“金叉死叉”而在于背离
  • 原文地址:https://www.cnblogs.com/bacazy/p/5823744.html
Copyright © 2011-2022 走看看