angularjs中的transformRequest是什么

2023-12-23

我有一个代码

 transformRequest: function(obj) {
        var str = [];
        for(var p in obj)
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    }

我知道这段代码是更改序列化算法并使用内容类型“application/x-www-form-urlencoded”发布数据。但我不知道它的语法是什么。什么是obj在功能上。请为我解释一下。感谢


转换请求通常用于将请求数据转换为服务器可以轻松处理的格式(您的后端代码)。

例如 - 如果您想发送请求中经过一些修改的数据,那么您可以使用它。

       $scope.save = function() {
    $http({
        method: 'POST',
        url: "/Api/PostStuff",
        //IMPORTANT!!! You might think this should be set to 'multipart/form-data' 
        // but this is not true because when we are sending up files the request 
        // needs to include a 'boundary' parameter which identifies the boundary 
        // name between parts in this multi-part request and setting the Content-type 
        // manually will not set this boundary parameter. For whatever reason, 
        // setting the Content-type to 'undefined' will force the request to automatically
        // populate the headers properly including the boundary parameter.
        headers: { 'Content-Type': undefined},
        //This method will allow us to change how the data is sent up to the server
        // for which we'll need to encapsulate the model data in 'FormData'
        transformRequest: function (data) {
            var formData = new FormData();
            //need to convert our json object to a string version of json otherwise
            // the browser will do a 'toString()' on the object which will result 
            // in the value '[Object object]' on the server.
            formData.append("model", angular.toJson(data.model));
            //now add all of the assigned files
            for (var i = 0; i < data.files; i++) {
                //add each file to the form data and iteratively name them
                formData.append("file" + i, data.files[i]);
            }
            return formData;
        },
        //Create an object that contains the model and files which will be transformed
        // in the above transformRequest method
        data: { model: $scope.model, files: $scope.files }
    }).
    success(function (data, status, headers, config) {
        alert("success!");
    }).
    error(function (data, status, headers, config) {
        alert("failed!");
    });
};

};

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

angularjs中的transformRequest是什么 的相关文章

随机推荐

  • BeautifulSoup 选择具有特定类的某些元素中的所有 href

    我正在尝试从中删除图像网站 我尝试使用 Scrapy 使用 Docker 和 scrapy slenium Scrapy 似乎不适用于 windows10 home 所以我现在尝试使用 Selenium Beautifulsoup 我正在将
  • 模板类中的 C++ 非模板方法

    是否可以在 cpp 文件的模板类 结构 中编写非模板方法的实现 我读到模板方法应该写在 h上 但我的方法不是模板方法 尽管它属于模板类 这是我的 h 中的代码 include
  • BottomNavigationView 在片段事务上滞后

    问题 我正在使用BottomNavigationView来自我的一项活动的 Android 设计支持库 以及每个导航项的片段 每次我在栏上选择一个项目时 我都会执行一个片段事务 如下面的代码片段 为了简洁起见 删除了代码的某些部分 priv
  • Callkit - 如果从后台开始通话则没有音频

    从 iOS 13 3 1 开始出现这种情况 在我的应用程序中 我使用 Callkit WebRTC 建立 VOIP 连接 我总是能够毫无问题地建立连接 但是 从 13 3 1 开始 如果应用程序不在前台 我将无法启动 Callkit 调用
  • 来自特定网卡的 UDP 多播

    我正在寻找一些网络专家来帮助我解决问题 我有很多计算机运行我的软件 该软件使用 UDP 多播 如果计算机仅连接到一个网络 网络 A 则此方法可以正常工作 我的计算机 也运行上述软件 将在端口 XXXX 上侦听多播 这台计算机有两个网卡 当我
  • 如何缓存 Spring Data JPA 投影

    我正在使用 Spring Data JPA 存储库运行 Spring Boot 1 5 1 我已经在我的用户存储库中添加了一个方法 该方法使用 JPA 投影 UserProfile 效果很好 我现在希望在我的服务层中缓存该方法的结果 该层应
  • 从 Apache Spark 中的架构获取数据类型列表

    我在 Spark Python 中有以下代码 用于从 DataFrame 的架构中获取名称列表 该代码工作正常 但如何获取数据类型列表 columnNames df schema names 例如 类似 columnTypes df sch
  • 使用 NAudio 在 C# 中播放 ohLibSpotify pcm 数据流

    我正在尝试播放从 ohLibSpotify c 库提供的原始 pcm 数据 https github com openhome ohLibSpotify https github com openhome ohLibSpotify 我在以下
  • extjs:如何在列渲染器中使用ajax调用?

    我现在得到的是这样的 renderer function value var ret var conn new Ext data Connection conn request method POST url rsdBackend para
  • SOAP XML WS-Security 签名验证

    我能够使用 WS Security 签名的证书来签署 SOAP XML 但我无法验证其签名 验证签名时会导致异常 我们将不胜感激 希望提供一些帮助来解决该问题 SOAP 封装的 WS Security 签名 https docs oracl
  • 单例的实例类型与类名?

    据我了解 instancetype向编译器声明该方法的返回类型与接收消息的类相同 传统上 我总是声明我的单例初始值设定项 并将类名显式设置为返回类型 interface MyClass NSObject MyClass sharedInst
  • 使用jquery在ul中添加li元素

    我尝试制作简单的待办事项列表 这是我的 html 代码
  • jQuery UI 对话框关闭时刷新父页面

    因此 每次关闭 jQuery UI 中的特定对话框时 我都希望刷新父页面 我怎样才能做到这一点 jQuery 代码 document ready function var dlg createTeam dialog title Create
  • 在类构造函数中实例化供应商类

    在我的 CakePHP 2 应用程序中 我有这样的供应商 我需要在我的控制器类中创建此供应商类的实例 所以我将在控制器的不同功能中使用该实例 App import Vendor fancyVendor array file gt fancy
  • Emacs 中的文本转语音

    我不是瞎子 我只是想有一种方法让我的 Windows 机器大声读取缓冲区的内容 以下是基本要求 读取任何英文文本缓冲区 随时暂停阅读并随时恢复 而不是当有人走进我的办公室时等待几分钟等待大缓冲区完成 调整播放时的读回速度 突出显示当前正在阅
  • 特征:矩阵到四元数和后面有不同的结果

    我使用 Eigen 库将矩阵转换为四元数 但是当我将其中一个矩阵转换为四元数并将其烧回时 它变成了另一个矩阵 即单位矩阵 我使用的旋转矩阵是从变换矩阵分解而来的 Eigen Matrix3f R3d R topLeftCorner lt 3
  • 如何使用 WPF 中的资源图标?

    我有一个作为资源嵌入的 ico 文件 构建操作设置为资源 我正在尝试创建一个 NotifyIcon 如何引用我的图标 notifyIcon new NotifyIcon notifyIcon Icon my icon file is cal
  • 理解php中的浮点数

    我知道这些问题可能会被很多人问到 但从我的阅读和测试来看 它让我有点困惑 而且我所做的很多阅读让我更加困惑 因为它非常复杂 有些人似乎对简单的比较有问题 但我自己没有问题 例如 num1 27 64 num2 27 64 if num1 n
  • 重新部署 JRebel 的替代方案 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 JRebel 允许重新部署新编译的代码 而无需重新启动应用程序 我想知道是否有任何替代方案 免费 这FAQ http zeroturnar
  • angularjs中的transformRequest是什么

    我有一个代码 transformRequest function obj var str for var p in obj str push encodeURIComponent p encodeURIComponent obj p ret