2017-03-02 73 views
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 --> 

请我需要删除时,“对象”,我的问题就解决了解决此问题......非常感谢

+0

内部服务器错误是指:_check你的日志文件的详细信息_ – jszobody

+0

我在哪里可以找到它!? – khaledbelal

+0

@khaledbelal检查你的后端API,这个可能是'http:// localhost/etqan/etqan_lara/public/api/user',你可以使用POSTMAN客户端在你的前端应用程序中使用它们之前测试你的API。 –

回答

0

。感谢

register(data){ 
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)); 
}