Angular_http_post

作者: 就特么这么爱夏天 | 来源:发表于2018-01-15 10:31 被阅读0次

由于Angular http默认的post 提交是使用ajax的post请求,所以其 Content-Type为application/json。所以参数的携带位置是request Body当中,但是后台框架并不能对body中的携带参数进行解析。所以建议将Content-Type改为application/x-www-form-urlencoded 并封装

部分示例代码:

constructor(private http: Http, //private log: LogService

  ) {

    this.headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' });

    this.options = new RequestOptions({ headers: this.headers });

  }

/**

  * form post方法

  * @param url

  * @param requestParams

  */

  public hasHeaderPost(url: string, params: any) {

    return this.http.post(url, params,this.options).map(res => res.json());

  }

  /**

  * ajax post方法

  * @param url

  * @param requestParams

  */

  public post(url: string, params: any) {

    return this.http.post(url, params).map(res => res.json());

  }

如此获取参数的方法同get类型请求。

相关文章

  • Angular_http_post

    由于Angular http默认的post 提交是使用ajax的post请求,所以其 Content-Type为a...

网友评论

    本文标题:Angular_http_post

    本文链接:https://www.haomeiwen.com/subject/ansroxtx.html