为什么以下Angular 5代码中的请求标头未在请求中传递?

时间:2018-04-24 00:49:41

标签: javascript angular http post angular5

因此,我无法获取此http请求对象以及请求发送我的标头。

   loginUser(email,password){     
   console.log("loginuser");     
   let formData:FormData = new FormData();  
   formData.append("format","json");
   formData.append("email",email);
   formData.append("password",password);      
   let headers = new HttpHeaders();       
   headers.append('command', 'ProcessLoginPos');                    
   headers.append('Access-Control-Allow-Origin', '*');    
   const req = new HttpRequest('POST', 'http://localhost:8080/ProjectOne/', formData, {         'headers':headers,      reportProgress: true,       responseType: 'text'      });
return this.http.request(req,{},{"command": "ProcessLoginPos"});   
}

1 个答案:

答案 0 :(得分:0)

您的最后一行应遵循以下模式:

return this.http.post(url, body, { headers: headers, params: params })

最后你应该得到类似的东西:

return this.http.post('http://localhost:8080/ProjectOne/', formData, { headers: headers })

结果(但我不知道,你为什么需要new FormData()):

loginUser(email, password) {         
   let formData: FormData = new FormData();  
   formData.append("format", "json");
   formData.append("email", email);
   formData.append("password", password);      
   let headers = new HttpHeaders();       
   headers.append('command', 'ProcessLoginPos');                    
   headers.append('Access-Control-Allow-Origin', '*');    
   return this.http.post('http://localhost:8080/ProjectOne/', formData, { headers: headers })   
}