2017-06-12 52 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()); 
    }