I am trying to do a simple Check Login method using Angular 2(2.0.0-rc.1) in a service. When I attempt to set the content-type to application/json it never sets the headers when sending to my web api backend.
import { Injectable } from '@angular/core'; import { HTTP_PROVIDERS, Http, Response, Headers, RequestOptionsArgs, RequestMethod, Request, RequestOptions } from '@angular/http'; import { Observable } from 'rxjs/Rx'; import { Other Modules Left out for Security } from 'Hidden'; @Injectable() export class LoginService { private _http: Http; constructor(http: Http) { this._http = http; } CheckLogin(model: CredentialModel) { let url = 'http://localhost:51671/api/Login'; let data = JSON.stringify(model); let headers = new Headers(); headers.append('Content-Type', 'application/json'); headers.append('Accept', 'application/json'); let requestOptions = new RequestOptions({ method: RequestMethod.Post, url: url, headers: headers, body: data }); console.log(requestOptions); console.log(data); return this._http.post(url, data, requestOptions); } } Request from Web Api
OPTIONS /api/Login HTTP/1.1 Host: localhost:51671 Connection: keep-alive Pragma: no-cache Cache-Control: no-cache Access-Control-Request-Method: POST Origin: http://localhost:5616 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2754.0 Safari/537.36 Access-Control-Request-Headers: content-type Accept: */* DNT: 1 Referer: http://localhost:5616/ Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 Using the exact same data using PostMan works fine! Looks like there is an issue with Angular 2 sending application/json using the POST method.
Any help resolving this issue would be helpful. Open to trying any suggestions.
return this._http.post(url, data, {headers: headers});this._http.post(url, body, { headers })(haha, echonax :D)preflight request,angular2 cors