Http帖子,有凭据。我做错了什么?

时间:2016-06-01 13:07:05

标签: http angular xmlhttprequest

1.我无法使用useCredentials设置正确的请求。任何人都可以告诉我代码有什么问题?

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

import { Observable }     from 'rxjs/Observable';

@Injectable ()
export class StructureRequestService {
result: Object;
constructor (private http: Http) {

2.使用XHR对象 - 我认为问题出在这里。

    let _build = (<any> http)._backend._browserXHR.build;
    (<any> http)._backend._browserXHR.build = () => {
        let _xhr =  _build();
        _xhr.withCredentials = true;
        return _xhr;
    };
}
private myUrl = 'http://manny.herokuapp.com/audit/get/structure';

//create an http request
sendRequest() {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({
        headers: headers
       // , withCredentials: true
    });
    return this.http.post(this.myUrl,options)
        .map((res: Response) => res.json())
        .subscribe(res => {this.result = res;});
}
}

1 个答案:

答案 0 :(得分:0)

您在帖子请求中缺少帖子正文参数。它必须是一个字符串

 return this.http.post(this.myUrl, body, options)
  

     

尽管内容类型被指定为JSON,但POST主体实际上必须是一个字符串。因此,我们在将JSON英雄内容作为正文参数传递之前对其进行显式编码。

参考:https://angular.io/docs/ts/latest/guide/server-communication.html