2017-06-12 4 views
0

Für Post-Anfrage von Angular nach Laravel bekomme ich Fehler (Interner Serverfehler). Aber es funktioniert erfolgreich in Postman.Angular 2+ und Laravel - Interner Server Fehler

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 (Middleware)

 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'); 
    } 

In Angular 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 
    }); 
    } 

Mein Fehler ->POST http://localhost:8000/api/addHouse 500 (Internal Server Error)

+0

Hallo, willkommen in SO. Siehe [this] (https://stackoverflow.com/help/mcve) und [how to ask] (https://stackoverflow.com/help/how-to-ask) – rll

+0

Sie müssen wahrscheinlich csrf-Token in http liefern header –

+0

Meinst du Cors Middleware? Ich gebe es. –

Antwort

0

Ich löste es durch addHouse Funktion zu verändern. Danke an alle ..

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