我正在使用symfony 4开发API REST,并使用Ionic开发应用。
我刚刚通过Ionic App通过API测试了一些方法,我可以看到URL中添加了一些内容
我检索到的错误如下:
Object { headers: {…}, status: 0, statusText: "Unknown Error", url: "http://127.0.0.1:8000/api/v1/clock/in", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://127.0.0.1:8000/api/v1/clock/in: 0 Unknown Error", error: error }
clock-service.service.ts:23:8
您可以看到我传递的网址是http://127.0.0.1:8000/api/v1/clock/in,但是我收到的响应是针对http://127.0.0.1:8000/api/v1/clock/in:的Http失败响应,很显然,URL http://127.0.0.1:8000/api/v1/clock/in:返回了404错误。
我的代码有问题吗?
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { RequestOptions } from '@angular/http';
@Injectable({
providedIn: 'root'
})
export class ClockService {
constructor(public httpClient: HttpClient) {
}
setClock() {
let headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Bearer', 'the_bearer_here' );
const requestOptions = new RequestOptions({ headers: headers });
this.httpClient.post('http://127.0.0.1:8000/api/v1/clock/in', requestOptions)
.subscribe(data => {
console.log(data['body']);
}, error => {
console.log(error);
});
}
}
答案 0 :(得分:0)
问题与我的API中的CORS有关,这是我的错误。
感谢您的时间。
最终的角度代码:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {RequestOptions, Request, RequestMethod} from '@angular/http';
@Injectable({
providedIn: 'root'
})
export class ClockService {
constructor(public http: HttpClient) {
}
setClock() {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer the_bearer_code'
})
};
this.http.get('http://127.0.0.1:8000/api/v1/clock/in', httpOptions)
.subscribe(data => {
console.log(data['body']);
});
}
}