我有一个后端接口,我用我的角1.3应用程序调用它,没有问题。在我的角5应用程序中,我得到了一个HTTP 403 (禁止),我在图片中面对请求参数(左侧为1.3角,右侧为角5):

我的5角代码如下所示:
createDate(calendarEvent: CalendarEvent) {
let serialDates = false;
let calendarEventSerialDateType = 'NO_SERIAL_DATE';
let serialEndDate = this.utilService.convertDateToDateString(new Date());
let url: string = environment.apiEndpoint + 'calendarevents/calendarevent/' + serialDates + '/' + calendarEventSerialDateType + '/' + serialEndDate + '/';
let headers = new Headers({ 'Content-Type': 'application/json', 'X-AUTH-TOKEN': this.authService.getToken()});
let options = new RequestOptions({ headers: headers });
return this.http.post(url, calendarEvent, options).map(res => res.json()).subscribe(res => console.log(res));}
例如,我不知道为什么X-8月-令牌不是用角5设置的,因为我在headers对象中设置了
let headers = new Headers({ 'Content-Type': 'application/json', 'X-AUTH-TOKEN': this.authService.getToken()});以及为什么OPTIONS在请求方法中提到角为5而不是POST类似于角1.3。
有人知道我做错了什么吗?
发布于 2018-03-02 13:25:25
OPTIONS请求被视为飞行前请求,它是在实际请求之前发送的,以检查方法的存在性。
如果发送的请求是有效的,它将调用有效的方法。
对于请求头,您可以使用上述答案中的一个。
let Options = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};发布于 2018-03-01 07:05:37
let Options = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};返回this.http.post(url,calendarEvent,Options).map(res => res.json()).subscribe(res => console.log(res));
发布于 2018-03-19 12:44:28
对于Angular5,
import { HttpClient, HttpHeaders } from '@angular/common/http';
const headers = new HttpHeaders().set('X-AUTH-TOKEN', this.authService.getToken());'Content-Type': 'application/json'map。示例:
createDate(calendarEvent: CalendarEvent) {
let serialDates = false;
let calendarEventSerialDateType = 'NO_SERIAL_DATE';
let serialEndDate = this.utilService.convertDateToDateString(new Date());
let url: string = environment.apiEndpoint + 'calendarevents/calendarevent/' + serialDates + '/' + calendarEventSerialDateType + '/' + serialEndDate + '/';
let headers = new HttpHeaders().set('X-AUTH-TOKEN', this.authService.getToken());
return this.http.post(url, calendarEvent, {headers: headers}).subscribe(res => console.log(res));
}https://stackoverflow.com/questions/49003640
复制相似问题