Laravel 5.6 API在Post方法中从Ajax调用时未返回

时间:2018-08-28 13:54:35

标签: php ajax laravel-5.6

我做了一个post方法来提供json数据作为api端点

我的路线是

Route::post('checkFuzzy', 'SolrController@checkFuzzy')->middleware('cors');

我让 cors 验证了我的血统,但是ajax post方法没有给出任何信息 ajax如下。

$('.searchme').on('click',function(){
    var company = $("input[name=Company]").val();
    console.log(company)
    $.ajax({            
        headers: {
            'Content-Type': 'text/html',
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
        },
        crossOrigin: true,
        type: "post",
        url: 'http://127.0.0.1:8006/api/checkFuzzy',
        data: { 
            Company: function(){
                return company
            },
        },
        //data:'Company='+ company+'&Individual='+ individual,
        success: function(msg){
        alert('wow' + msg);
     }
    });
});

,请求标头中的响应如下

Provisional headers are shown
Accept: */*
Content-Type: text/html
Origin: http://localhost:8006
Referer: http://localhost:8006/searchSolr
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36
X-CSRF-TOKEN: e522xOu6IhXGCXLDyw929zCogVcXcaZ5yYWwXDo4

Cors.php如下

 public function handle($request, Closure $next)
    {
        $response = $next($request);
        $response->headers->set('Access-Control-Allow-Origin' , '*');
        $response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE');
        $response->headers->set('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application, x-xsrf-token, x-csrf-token');
        return $response;
    }

状态码为200。

1 个答案:

答案 0 :(得分:0)

问题在于返回类型。返回类型应该是json而不是数组。

        return response()->json([
            'status' => 'success',
            'result' => $calCulatedValue
        ]);