2017-06-12 69 views
0

对于从Angular发送到Laravel的邮件请求,我收到错误消息(内部服务器错误)。但它在邮差中成功运作。Angular 2+和Laravel - 内部服务器错误

api.php

<?php 
    use Illuminate\Http\Request; 

    Route::post('/addHouse', [ 
     'uses' => '[email protected]' 
    ]); 

HouseController.php

public function postHouse(Request $request) 
{ 
    $house=new House(); 
    $house->houseName=$request->input('houseName'); 
    $house->type=$request->input('type'); 
    $house->for=$request->input('for'); 
    $house->address=$request->input('address'); 
    $house->price=$request->input('price'); 
    $house->description=$request->input('description'); 
    $house->bedrooms=$request->input('bedrooms'); 
    $house->bathrooms=$request->input('bathrooms'); 
    $house->area=$request->input('area'); 
    $house->save(); 
    return response()->json([ 
     'house'=>$house 
    ],201); 
} 

Cors.php(中间件)

 public function handle($request, Closure $next) 
    { 
     return $next($request) 
      ->header('Access-Control-Allow-Origin','*') 
      ->header('Access-Control-Allow-Methods','Get, POST, PUT, PATCH, DELETE, OPTIONS') 
      ->header('Access-Control-Allow-Headers', 'Content-type, Authorization'); 
    } 

在角house.service.ts

addHouse(content) { 
     console.log(content); 
     const body = JSON.stringify({ 
     content: content 
    }); 
    const headers = new Headers({ 
     'Content-Type':'application/json' 
    }); 
    return this._http.post('http://localhost:8000/api/addHouse', body, { 
     headers: headers 
    }); 
    } 

我的错误 - >POST http://localhost:8000/api/addHouse 500(内部服务器错误)

+0

嗨,欢迎来到SO。请参阅[this](https://stackoverflow.com/help/mcve)和[如何提问](https://stackoverflow.com/help/how-to-ask) – rll

+0

您可能需要在http中提供csrf标记标题 –

+0

你的意思是Cors中间件?我正在给它。 –

回答

0

我改变addHouse功能解决了这个问题。谢谢大家..

addHouse(data) { 
     var headers = new Headers(); 
     headers.append (
      'Content-type','application/json' 
     ); 
    return this._http.post('http://localhost:8000/api/addHouse', JSON.stringify(data), { 
        headers:headers 
        }).map(res => res.json()); 
    }