Angular 2、Http和未设置内容类型;应用程序/json“;
我正在尝试在服务中使用Angular 2(2.0.0-rc.1)执行一个简单的检查登录方法。当我尝试将内容类型设置为application/json时,它在发送到我的web api后端时从不设置头Angular 2、Http和未设置内容类型;应用程序/json“;,angular,http-headers,Angular,Http Headers,我正在尝试在服务中使用Angular 2(2.0.0-rc.1)执行一个简单的检查登录方法。当我尝试将内容类型设置为application/json时,它在发送到我的web api后端时从不设置头 import { Injectable } from '@angular/core'; import { HTTP_PROVIDERS, Http, Response, Headers, RequestOptionsArgs, RequestMethod, Request, RequestOption
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);
}
}
来自Web Api的请求
使用完全相同的数据使用邮递员很好!看起来Angular 2使用POST方法发送应用程序/json时存在问题
解决这个问题的任何帮助都会很有帮助。可以尝试任何建议。Http类的
post
方法接受类型为RequestOptionsArgs
的对象,而不是RequestOptions
class RequestOptionsArgs {
url : string
method : string | RequestMethod
search : string | URLSearchParams
headers : Headers
body : any
withCredentials : boolean
}
您可以在第三个参数中以这种方式指定它:
return this._http.post(url, data, { headers: headers });
RequestOptionsArgs
是Angular2中的一个接口,因此您可以将RequestOptions
指定为一个值。在Angular4和更大的角度中设置标题现在应该这样设置:
let headers=新的HttpHeaders({
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”
});
this.http.post(“apirl”,data,{headers:this.headers})您能在没有请求选项的情况下尝试吗?i、 返回这个代码>我就是这样做的:
这个。_http.post(url,body,{headers})
(哈哈,echonax:D)可能是您必须允许CORS请求,或者搜索飞行前请求
,angular2 CORS
我在这里尝试了每一步,但仍然得到相同的错误。对于那些已经使用它的人来说,您是否使用了最新的Angular2 RC1?在never angular 2版本中是否发生了更改,因为RequestOptions现在在我的代码中起作用了!只需记住导入{Headers}
return this._http.post(url, data, { headers: headers });