加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在Angular2中使用httpparamserializer

发布时间:2020-12-17 06:51:32 所属栏目:安全 来源:网络整理
导读:我想在Angular2中使用httpparamserializer.我google了很多,但这些例子仅适用于angular1,如下所示. How to inject $httpParamSerializer for use in templateUrl 在Angular2中使用它的语法是什么? 解决方法 我有完全相同的问题 HERE 我的解决方案:我有一个P
我想在Angular2中使用httpparamserializer.我google了很多,但这些例子仅适用于angular1,如下所示.
How to inject $httpParamSerializer for use in templateUrl

在Angular2中使用它的语法是什么?

解决方法

我有完全相同的问题 HERE

我的解决方案:我有一个POST,我将数据发布到Web服务,我序列化我的对象,这就是我的工作方式.它非常简单易用.

URLSearchParams

基本的例子

let params = new URLSearchParams();
params.set('search',term); // the user's search value

Set Search Parameters

我的外形component.ts

import { Headers,RequestOptions,Http,Response,URLSearchParams } from '@angular/http';


// User is done editing,serialize and POST to web service
saveEdits(): void {
    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    let options = new RequestOptions({ headers: headers });

    // Dynamically serialize the entire object
    // *** THIS IS THE SERIALIZATION ***
    let params: URLSearchParams = this.serialize(this.selectedItem);


    this._http.post('http://URLtoPOSTobjTo/path',params,options)
      .map(this.extractData)
      .catch(this.handleError);

}

/**
 * Serializes the form element so it can be passed to the back end through the url.
 * The objects properties are the keys and the objects values are the values.
 * ex: { "a":1,"b":2,"c":3 } would look like ?a=1&b=2&c=3
 * @param  {SystemSetup} obj - The system setup to be url encoded
 * @returns URLSearchParams - The url encoded system setup
 */
serialize(obj: SystemSetup): URLSearchParams {
    let params: URLSearchParams = new URLSearchParams();

    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            var element = obj[key];

            params.set(key,element);
        }
    }
    return params;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读