douyeyan0650 2018-08-28 13:54
浏览 214

Laravel 5.6 api在post方法中从ajax调用时没有返回

I made a post method to supply json data as api endpoint

My route is

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

I made cors validate my origin but the ajax post method is not giving anything the ajax is as follows.

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

and the response in request headers is as follows

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

the Cors.php is as follows

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

The status code is 200.

  • 写回答

1条回答 默认 最新

  • dongxian6285 2018-08-29 18:40
    关注

    The problem was with the return type. The return type should be a json not an array.

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

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算