【问题标题】:why JSON return html为什么 JSON 返回 html
【发布时间】:2019-03-05 01:21:08
【问题描述】:

我正在使用 Laravel 5.1。我尝试通过控制器获取一些 JSON 格式的数据,但它返回的是 html 而不是 json。

create.blade.php

{!! Form::open(['url' => 'schedule']) !!}

@include('schedule.form',['submitButtonText'=>'Submit'])
{!! Form::close() !!}

edit.blade.php

   {!! Form::model($schedule,['method'=>'PATCH','url' => 'schedule/'. $schedule->scheduleID ]) !!}
    @include('schedule.form',['submitButtonText'=>'Update'])

    {!! Form::close() !!}

schedule.form 中的 Ajax

 $.post('../schedule/loadRoute',{routeID:routeID},function(r){
        console.log(r);
        $.each(JSON.parse(r), function(key, value){ 

               coordinates.push(new google.maps.LatLng(value.lat,value.lon));
               if(value.is_station==1){
                   addMarker(new google.maps.LatLng(value.lat,value.lon),value.name);
               }

        });

        clearMap();

    }); 

控制器中的loadRoute函数

public function loadRoute(Request $request){

   $routeID=$request->get('routeID');



    $station=Station::where('route_id',$routeID)->get()->toArray();


    echo json_encode($station);

}

编辑

routes.php

 Route::group(
    ['middleware' => ['web']],
    function () {
        Route::auth();

        Route::get('schedule/getScheduleByRouteID/{routeID}', 'ScheduleController@getScheduleByRouteID');

        Route::resource('schedule', 'ScheduleController', ['except' => ['destroy', 'show']]);

            Route::post('schedule/loadRoute','ScheduleController@loadRoute');
});

创建和编辑页面共享相同的 schedule.form,但 JSON 数据仅在创建页面中成功返回,对于编辑页面,它返回 html 而不是 JSON,我在控制台中收到此错误(未捕获 SyntaxError:意外令牌

两个页面使用相同的表单,但为什么在编辑页面时不起作用?

【问题讨论】:

  • 可能是因为服务器由于某些服务器错误而返回 HTML,或者它实际上应该这样做而您访问不正确
  • Ajax in schedule.form 部分是在刀片文件还是 javascript 文件中?
  • 它在刀片文件脚本部分中

标签: json laravel


【解决方案1】:

我想您看到此问题的原因之一是因为您的 loadRoute 路由位于 resource 路由下方。

尝试将顺序更改为:

Route::get('schedule/getScheduleByRouteID/{routeID}', 'ScheduleController@getScheduleByRouteID');

Route::post('schedule/loadRoute','ScheduleController@loadRoute');

Route::resource('schedule', 'ScheduleController', ['except' => ['destroy', 'show']]);

https://laravel.com/docs/5.2/controllers#restful-supplementing-resource-controllers

另外,你应该从控制器 return 而不是 echo

public function loadRoute(Request $request)
{
    return Station::where('route_id', $request->get('routeID'))->get();
}

在上面Laravel会自动回复json_encode()并添加相应的headers。

通过您的$.post 电话,我会将其更改为以下内容:

$.post('{{ url('schedule/loadRoute') }}', {routeID: routeID, _token: '{{ csrf_token() }}'}, function (r) {
    console.log(r);

    $.each(r, function (key, value) {

        coordinates.push(new google.maps.LatLng(value.lat, value.lon));
        if (value.is_station == 1) {
            addMarker(new google.maps.LatLng(value.lat, value.lon), value.name);
        }

    });

    clearMap();

}, 'json');

这是因为 Laravel 现在将返回正确的 json 响应,并带有正确的标头,因此您不需要 JSON.parse() 它。此外,您似乎没有提供csrf_token,因此它也已添加到数据对象中。

希望这会有所帮助!

【讨论】:

  • 哇,谢谢!这解决了我的问题。忘了提,我添加了标题和令牌
猜你喜欢
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多