我使用Laravel 5.8和Laravel Passport来处理我的后端和API代码,而我将Ionic4-Angular8用于我的前端Android + iOS应用。
我正在尝试将包含base64编码图像的formData作为POST请求从ionic-angular应用发送到我的Laravel API后端。这指向localhost/api/user/services/create
,但出现以下错误:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException:此路由不支持GET方法。支持的方法:POST。
这是我的路线文件的相关部分:
// Routes file
Route::group(['prefix' => 'user'], function () {
Route::group(['middleware' => ['auth:api']], function () {
Route::post('/services/create', 'Api\UserServiceController@userDashboardServicesCreatePost');
});
});
离子角API调用:
async onSubmitCreateServiceForm(form: NgForm) {
if (form.valid) {
//START PROCESS FORM DATA
const formData = new FormData();
var prop;
for (prop in this.datashareService.registerformfields) {
formData.append(prop, this.datashareService.registerformfields[prop]);
}
//Start Connect to API
const headers = new HttpHeaders({
'Authorization': this.authService.token["token_type"] + " " + this.authService.token["access_token"]
});
let promise = new Promise((resolve, reject) => {
this.http
.post(`${this.env.API_URL}user/services/create`, formData, {headers: headers})
.toPromise()
.then(
async res => {
this.datafromapi = res;
this.alertService.presentToast(this.datafromapi.msg);
this.navCtrl.navigateForward('my-services');
},
error => {
console.log(error.error.errors);
this.alertService.presentToastError(error.error.message);
}
);
});
//End Connect to API
}
//END PROCESS FORM DATA
}
当我提交表单而不选择图像时,它会成功通过。但是,当选择图像并提交表单时,出现上述错误。
我有什么想念的吗?如何通过离子角度应用程序将图像上传到Laravel服务器?