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'
        }
      });
    });
    
  • 相关阅读:
    linq to sql 扩展方法
    跨线程的安全更新控件
    WinForm程序启动控制台窗口Console
    Winfrom巧用Using设置鼠标为WaitCursor
    在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。
    基于Token的身份验证——JWT(转)
    jwt算法
    session问题总既然(深入理解)&Token问题理解&sso单点登陆理解实现
    1.spring boot要求最低jdk1.8,平安默认1.6问题,-》安装JDK1.8 2.maven 3.3.3要求最低jdk1.7->安装jdk 1.8
    批量插入删除
  • 原文地址:https://www.cnblogs.com/bacazy/p/5823744.html
Copyright © 2011-2022 走看看