duankuaiwang2706 2016-09-11 17:13
浏览 25

在laravel上回归json

Uncaught SyntaxError: Unexpected token < in JSON at position 0

I need to return json, send it throw $.get request and show it with .html funtion. I would prefer dom option, but i have a laravel framework on backend, and i'm still using blade. So i have something like:

                $response = [
                    'success'   => true,
                    'content'   => view('/collette/create_step_01', compact('prodotto', 'destinatario', 'input'))->render(),
                ];

and i return

return json_encode($response);

There is a better way for return json on laravel?

On js i have

    var qs = getQueryString();

if (typeof(step) === 'undefined') {
    initColletta(); //define step
}

$.get(url + 'collette/create/steps/' + step, qs, function(data) {
    var res = JSON.parse(data);

    if (res.success) {
        showModalBody(res.content);

        if (typeof(res.stop) !== 'undefined' && res.stop) {
            $('#step-next').attr('href', res.link).unbind('click');
        }
    }
});
  • 写回答

2条回答 默认 最新

  • dounai1986 2016-09-11 17:20
    关注

    To return Json in Laravel you should use

    return response()->json(your_json_here);
    

    And as far as the error is concerned that is likely due to the fact that you don't need to Parse the JSON as it is already a valid JSON object.

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题