0
當我發佈表單角度表單到API Laravel時,我在控制檯中得到錯誤500(內部服務器錯誤)....爲什麼?API Laravel 5.4和Angular 2之間的錯誤500(內部服務器錯誤)
注意:後端API與POSTMAN完美配合。
我Cors.php在Laravel是:
namespace App\Http\Middleware;
use Closure;
class Cors
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Credentials','true')
->header('Access-Control-Allow','GET,POST,PUT,PATCH,DELELE,OPTIONS')
->header('Access-Control-Allow-Headers','Content-Type, Authorization,X-XSRF-TOKEN,X-CSRF-TOKEN','CSRF-TOKEN','XSRF-TOKEN');
}
}
和register.component.ts:
import { Component,OnInit,Input } from '@angular/core';
import { Router } from '@angular/router';
import { NgForm } from '@angular/forms';
import { UserService } from '../../services/user.service';
@Component({
styleUrls: ['../../templates/users/register.component.css'],
templateUrl: '../../templates/users/register.component.html',
})
export class RegisterComponent {
title = 'register';
constructor(private _router: Router,private UserService: UserService){
}
ngOnInit() {
}
onSubmit(form:NgForm){
this.UserService.register(form.value);
}
}
和user.service.ts:
import { Injectable } from '@angular/core';
import { Http,Response,Headers } from '@angular/http';
import 'rxjs/Rx';
import { Observable } from 'rxjs';
@Injectable()
export class UserService{
constructor(private http: Http){
}
register(data : Object){
const body = JSON.stringify({data:data});
const headers = new Headers({'Content-Type':'application/json'});
return this.http.post('http://localhost/etqan/etqan_lara/public/api/user',body,{headers:headers}).subscribe((data2) => console.log(data2));
}
}
和註冊.component.html是:
<!-- Header -->
<header>
<div class="container">
<div class="intro-text wow fadeInDown animated">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="wrap">
<p class="form-title">مستخدم جديد</p>
<form class="login" #f="ngForm" (ngSubmit)="onSubmit(f)">
<input type="text" placeholder="اسم المستخدم" name="username" ngModel />
<input type="text" placeholder="البريد الالكتروني" name="email" ngModel />
<input type="password" placeholder="كلمة المرور" name="password" ngModel />
<input type="password" placeholder="تأكيد كلمة المرور" name="repassword" ngModel />
<input type="submit" value="تسجيل" class="btn btn-success btn-sm" />
</form>
</div>
</div>
</div>
</div>
</div>
</header>
<!-- Header End -->
請我需要刪除時,「對象」,我的問題就解決了解決此問題......非常感謝
內部服務器錯誤是指:_check你的日誌文件的詳細信息_ – jszobody
我在哪裏可以找到它!? – khaledbelal
@khaledbelal檢查你的後端API,這個可能是'http:// localhost/etqan/etqan_lara/public/api/user',你可以使用POSTMAN客戶端在你的前端應用程序中使用它們之前測試你的API。 –