2016-03-04 10 views
0

我試圖從POSTMAN重新創建這些設置,允許我成功驗證到我的服務器。然而,在Angular 2中,我沒有看到如何在'application/json'中設置'raw'設置,因爲它需要在兩個地方:一個在Headers中,另一個在Body中。在Angular 2中重新創建郵差設置

enter image description here

//Config file 
import { Headers } from 'angular2/http'; 
export class Config { 
baseAPIUrl: string = 'http://api:8080/'; 
headers: Headers 

constructor() { 
    this.headers = new Headers(); 
    this.headers.append('Access-Control-Allow-Origin', 'http://api:8080'); 
    this.headers.append('Content-Type', 'application/json'); 
} 

// Session service 
login(email: string, password: string) { 
    var sessionSubscription = this.http.post(this.config.baseAPIUrl + 'auth/session', JSON.stringify({ email , password }), { 
    headers: this.config.headers }); 
    return sessionSubscription.subscribe((res) => { 
     console.log(res); 
    }, 
    (error) => console.log('error', error)); 
} 

回答

0

這些單選按鈕的請求的預覽剛剛設置。實際數據從客戶端發送的JSON(這也是服務器如何接收他們),你的指定如下:

this.headers.append('Content-Type', 'application/json'); 

要發送的數據爲純文本應設置Content-Type標題是這樣的:

this.headers.append('Content-Type', 'text/plain'); 

產地標題:

this.headers.append('Access-Control-Allow-Origin', 'http://api:8080'); 

沒有影響這裏,你應該在服務器端設置Access-Control-Allow-Origin

+0

它們是兩個單獨的文件。我通過'this.config'訪問所有配置。只是當我將JSON(應用程序/文本)更改爲其他任何內容時,它會給我一個錯誤:Status 422 Unprocessable Entity – Ade

+0

'JSON(application/text)'?不知道你在做什麼。 – Sasxa

+0

抱歉,我的意思是'JSON(application/json)' – Ade