反应本机-获取方法发布到Laravel返回异常MethodNotAllowedHttpException

时间:2018-10-08 08:17:01

标签: php laravel react-native axios

我正在尝试在React Native中向Laravel api发出带有访存的发布请求,它将此错误返回给我。

{'message':"",'exception':Symfony\\Component\\HttpKernel\\Exception\\MethodNotAllowedHttpException","file":....

Api.js(本机反应):

'use strict';

var baseUrl = 'http://dot.test.192.168.1.125.xip.io/api/';

export default {

    //token = Bearer OxtHDFRWuCxWpIK9omt30fG2RivWeFtAY8VWOzlPcYh9WvX6P8Jq7OrFU4hh
    post: function(url, data, token) {
        let opt = {
            method: 'post',
            body: JSON.stringify(data),
            headers: new Headers({
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                //'Content-Type': 'application/x-www-form-urlencoded'
            })
        };

        return fetch(baseUrl + url, opt);
    }

};

Laravel-

api.php

Route::namespace('Api')->middleware('auth:api')->group(function () {
   Route::post('expert/{id}/feedback', 'FeedbackController@ask');
});

feedbackController.php

<?php

namespace App\Http\Controllers\Api;

use App\User;
use Illuminate\Http\Request;
use App\Http\Requests\RequestFeedback;
use App\Http\Requests\Feedback\PostFeedbackAnswer;
use App\Services\FeedbackService;
use App\Http\Controllers\Controller;


class FeedbackController extends Controller
{
    private $feedbackService;

    public function __construct(FeedbackService $feedbackService)
    {     
        $this->feedbackService = $feedbackService;
    }

    public function ask(RequestFeedback $request, $expertId)
    {
        $feedback = $this->feedbackService->ask($request->only('text', 'project_id'), $expertId);

        return $feedback;
    }

}

我用

更改VerifyCsrfToken
protected $except = [
   'api' => '*',
   '/api/*'
];

我不明白为什么它不起作用...

我不会发布更多不允许我发送问题的代码。

1 个答案:

答案 0 :(得分:0)

POST更改为GET

var baseUrl = 'http://dot.test.192.168.1.125.xip.io/api/';

export default {

    //token = Bearer OxtHDFRWuCxWpIK9omt30fG2RivWeFtAY8VWOzlPcYh9WvX6P8Jq7OrFU4hh
    post: function(url, data, token) {
        let opt = {
            method: 'GET',
            body: JSON.stringify(data),
            headers: new Headers({
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                //'Content-Type': 'application/x-www-form-urlencoded'
            })
        };

        return fetch(baseUrl + url, opt);
    }

};