dtest84004 2017-06-06 16:38
浏览 192
已采纳

Laravel AJAX控制器和数据返回

I am making an AJAX call from a view:

function compareProducts() {
    var productIds = [];
    $('#compare-widget tbody tr').each(function(i, ele) {
        productIds[i] = $(ele).data('product-id');
    });
    $.ajax({
        url: 'product/compare',
        type: 'POST',
        data: {
            "_token": "{{ csrf_token() }}",
            productIds: productIds
        },
        success: function(result) {
        }
    });
}

which is triggering the controller:

public function compare(Request $request)
{
    $products = Product::whereIn('id', $request->productIds)->get();
    return view('compare', compact('products'));
}

but it is not doing anything in browser directly. When I open chrome inspector I see a OK 200 code, and when I open triggered link there, it shows fine, but I am not able to trigger redirection with AJAX.

If I do a window.location.href = "compare"; I get redirected to right page, but I no longer have access to $products

  • 写回答

2条回答 默认 最新

    报告相同问题?

    相关推荐 更多相似问题

    悬赏问题

    • ¥15 CMD 生成TXT内容输出成一行
    • ¥20 关于#pandas#的问题,如何解决?(语言-python)
    • ¥15 eclipse为什么导入不了RAR阿
    • ¥30 c语言设计井字棋小游戏
    • ¥15 关于属性关联的双极容度多属性决策vikor方法的公式
    • ¥15 10吨190马力爆炸品运输车,以80km/h的速度行驶1个小时输出的动能或者做的功是多少?
    • ¥15 《phthon编程从入门到实践》第十八章,从django入手,learning_log,ModuleNotFoundError: No module named 'learning_logs
    • ¥15 echarts 放入折叠面板宽高的问题
    • ¥50 求帮配置Django项目配置环境
    • ¥20 画出下列系统的bode图和乃式图