dougu3290 2017-04-23 21:17
浏览 57

Laravel:我可以用json编码参数以某种方式缩短这个url吗?

I have an AJAX request to grab some values from database and then redirect back to page with those values so I can use them on page.

The thing is that this URL is very long:

http://ibpc.dev/admin/products/create?specifications={%22id%22:4,%22title%22:%22Video%20Cards%22,%22created_at%22:null,%22updated_at%22:null,%22specifications%22:[{%22id%22:1,%22name%22:%22Model%22,%22created_at%22:%222017-04-23%2022:54:04%22,%22updated_at%22:%222017-04-23%2022:54:04%22,%22pivot%22:{%22category_id%22:4,%22specification_id%22:1},%22attributes%22:[{%22id%22:1,%22specification_id%22:1,%22name%22:%22Brand%22,%22created_at%22:%222017-04-23%2022:54:05%22,%22updated_at%22:%222017-04-23%2022:54:05%22}]},{%22id%22:2,%22name%22:%22Interface%22,%22created_at%22:%222017-04-23%2022:54:04%22,%22updated_at%22:%222017-04-23%2022:54:04%22,%22pivot%22:{%22category_id%22:4,%22specification_id%22:2},%22attributes%22:[{%22id%22:2,%22specification_id%22:2,%22name%22:%22Interface%22,%22created_at%22:%222017-04-23%2022:54:05%22,%22updated_at%22:%222017-04-23%2022:54:05%22}]},{%22id%22:3,%22name%22:%22Chipset%22,%22created_at%22:%222017-04-23%2022:54:04%22,%22updated_at%22:%222017-04-23%2022:54:04%22,%22pivot%22:{%22category_id%22:4,%22specification_id%22:3},%22attributes%22:[{%22id%22:3,%22specification_id%22:3,%22name%22:%22Chipset%20Manufacturer%22,%22created_at%22:%222017-04-23%2022:54:05%22,%22updated_at%22:%222017-04-23%2022:54:05%22},{%22id%22:4,%22specification_id%22:3,%22name%22:%22GPU%22,%22created_at%22:%222017-04-23%2022:54:05%22,%22updated_at%22:%222017-04-23%2022:54:05%22},{%22id%22:5,%22specification_id%22:3,%22name%22:%22Core%20Clock%22,%22created_at%22:%222017-04-23%2022:54:05%22,%22updated_at%22:%222017-04-23%2022:54:05%22}]},{%22id%22:4,%22name%22:%22Memory%22,%22created_at%22:%222017-04-23%2022:54:04%22,%22updated_at%22:%222017-04-23%2022:54:04%22,%22pivot%22:{%22category_id%22:4,%22specification_id%22:4},%22attributes%22:[{%22id%22:6,%22specification_id%22:4,%22name%22:%22Effective%20Memory%20Clock%22,%22created_at%22:%222017-04-23%2022:54:05%22,%22updated_at%22:%222017-04-23%2022:54:05%22},{%22id%22:7,%22specification_id%22:4,%22name%22:%22Memory%20Size%22,%22created_at%22:%222017-04-23%2022:54:05%22,%22updated_at%22:%222017-04-23%2022:54:05%22},{%22id%22:8,%22specification_id%22:4,%22name%22:%22Memory%20Interface%22,%22created_at%22:%222017-04-23%2022:54:05%22,%22updated_at%22:%222017-04-23%2022:54:05%22},{%22id%22:9,%22specification_id%22:4,%22name%22:%22Memory%20Type%22,%22created_at%22:%222017-04-23%2022:54:05%22,%22updated_at%22:%222017-04-23%2022:54:05%22}]},{%22id%22:5,%22name%22:%22Ports%22,%22created_at%22:%222017-04-23%2022:54:04%22,%22updated_at%22:%222017-04-23%2022:54:04%22,%22pivot%22:{%22category_id%22:4,%22specification_id%22:5},%22attributes%22:[]},{%22id%22:6,%22name%22:%22Details%22,%22created_at%22:%222017-04-23%2022:54:04%22,%22updated_at%22:%222017-04-23%2022:54:04%22,%22pivot%22:{%22category_id%22:4,%22specification_id%22:6},%22attributes%22:[]}]}

Can I somehow shorten this URL?

AJAX GET request from template:

('#category-select').change(function () {
    $.ajax({
        type: 'GET',
        url: '/admin/catalog/specifications',

        data: {
            selectFieldValue: $(this).val()
        },

        success: function (response) {
            window.location.href = response.redirectUrl + response.data;
        }
    });
});

Routes:

Route::get('/admin/products/create', [
    'uses' => 'AdminController@getCreateProduct',
    'as' => 'admin.getCreateProduct'
]);

Route::get('/admin/products/create/{specifications}', [
    'uses' => 'AdminController@getCreateProduct',
    'as' => 'admin.getCreateProduct'
]);

Route::get('/admin/catalog/specifications', [
    'uses' => 'AdminController@getProductSpecifications',
    'as' => 'admin.getProductSpecifications'
]);

AdminController:

public function getCreateProduct(Request $request)
{
    $categories = Category::all();

    if ($request['specifications']) {
        $specifications = json_decode($request['specifications']);
    } else {
        $specifications = null;
    }

    return view('admin.products.create', ['categories' => $categories, 'specifications' => $specifications]);
}

public function getProductSpecifications(Request $request)
{
    $categoryId = $request['selectFieldValue'];

    $specifications = Category::with('specifications.attributes')->find($categoryId);

    return response()->json(array('data' => json_encode($specifications), 'redirectUrl'=> '/admin/products/create?specifications='), 200);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题