HTTP Post in angular 2 with laravel 5.3 backend



我有两个具有相同属性的表(大学表和高中表)。我想做的是将数据从大学表发送到高中表。例如,我在大学表中有达特莫学院,如果我按下一个名为sendtoHighSchool()的按钮,Datmor College应该发布到高中表格。

当我单击"发送到高中"按钮时,我看到数据已正常获取,但我仍然收到此错误

" POST http://localhost:3000/api/college_api/v1/colleges 500 (Internal Server Error)
error_handler.js:47 EXCEPTION: Response with status: 500 Internal Server Error for URL: http://localhost:3000/api/college_api/v1/colleges 
ErrorHandler.handleError @ error_handler.js:47
next @ application_ref.js:272
schedulerFn @ async.js:82
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:74
NgZone.triggerError @ ng_zone.js:278
onHandleError @ ng_zone.js:257
ZoneDelegate.handleError @ zone.js:236
Zone.runTask @ zone.js:157
ZoneTask.invoke @ zone.js:335
Subscriber.js:227 Uncaught Response {_body: "<!DOCTYPE html>↵<html>↵    <head>↵        <meta ch…l>↵</div>↵↵            </div>↵    </body>↵</html>", status: 500, ok: false, statusText: "Internal Server Error", headers: Headers…}
SafeSubscriber.__tryOrUnsub @ Subscriber.js:227
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:74
NgZone.triggerError @ ng_zone.js:278
onHandleError @ ng_zone.js:257
ZoneDelegate.handleError @ zone.js:236
Zone.runTask @ zone.js:157
ZoneTask.invoke @ zone.js:335"

//桌子

<td><a class="btn btn-success" (click)="addToHighSchool()"><em class="fa fa-plus"></em></a>Send to High school</td>

学院组件

addToHighSchool(college) {
    this.httpService.add(college)
    .subscribe(data =>{
        console.log(data)
    });
    console.log(college)
}

//服务

add(user:any) {
    const body = JSON.stringify(user);
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http.post('http://localhost:3000/api/college_api/v1/colleges', body, {headers: headers})
    .map((data:Response) => data.json());
}

app/Http/Middleware/VerifyCsrfToken.php找到这个并进行写保护

$except = [ 'api/*' ];

然后查找路线并修改

<?php Route::group(['prefix' => 'api', 'middleware' => ['cors']], function(){ Route::resource('courses', 'CourseController', ['except' => [ 'create', 'edit' ]]); });

最新更新