2017-08-09 72 views
0

http請求發送請求我需要發送用戶名和密碼到登錄api使用角度http post請求。這裏是我的代碼:我如何傳遞用戶名和密碼在角度4

const params = new URLSearchParams(); 
    params.set('username', username); 
    params.set('password', password); 
    this.headers = new Headers({ 'content-type': 'application/x-www-form-urlencoded', 'authorization': 'Basic' }); 
    this.options = new RequestOptions({headers: this.headers, method: RequestMethod.Post, params: params }); 
    return this.http.post(this.config.baseUrlAuthSvc + this.config.login, this.options) 
     .map((response) => { return response; }) 

我收到以下錯誤響應:

請求URL:http://111.22.333.44:55/api/token 請求方法:OPTIONS 狀態代碼:400錯誤的請求 遠程地址:172.31.131.13 :85 推薦人政策:no-referrer-when-downgrade

我是否錯過了什麼?

+0

您可能需要將請求主體設置爲'username = enteredUsername&password = enteredPassword' –

+0

@jason是的,這就是我所做的。 params應該用於獲得請求 – Maverik

回答

-1

你能有這樣嘗試

return $http.post(URL, data, { 
     headers: { 
      "Content-Type": "application/x-www-form-urlencoded" 
     } 
    }).success(function (response) { 
     return response; 
    }); 

數據的輸入參數例如如下所示

var data = "username=" + username + "password=" + password; 
+0

這個問題是關於[標籤:angular]不是[標籤:angularjs] ... – n00dl3

+0

我錯過了棱角4,這個答案將有助於在1.x版本的情況下 –

+0

這個答案將是在1.x版本的情況下很有幫助 - 這很明顯 – Maverik

0

顯然,在一個post請求中,不可能在請求選項中傳遞params。 http.post請求中的第二個參數是body,所以我傳遞了字符串'username = xyz & password = 123'作爲第二個參數,它工作正常。

要點是,如果它是一個http post請求不使用params。

相關問題