将Ajax响应分配给php变量

时间:2016-08-08 10:38:51

标签: php jquery ajax laravel laravel-5.1

如果可能的话,如何将我的ajax响应分配给php变量?

在我的Laravel控制器中,我有这个方法:

public function editProductPost(Request $request)
{
    return response()->json([
        'sliderValue' => $request->get('value')
    ]);
}

这是我的Ajax:

    /**
     * Ajax Post
     */
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    $.ajax({
        type: 'post',
        contentType: "application/json",
        url: "{{ Route('editProductPost', $product->id) }}",
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        },
        data: JSON.stringify({ 
            value: getSliderVal,
            productId : getPrId
        }),
        datatype: 'json',
        success: function(response) {
            // get response
            console.log(response.sliderValue)
        }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你想做这样的事情:

public function editProductPost(Request $request)
{
    // Create your JSON response
    $response = response()->json([
        'sliderValue' => $request->get('value')
    ]);

    // Assign the content of the response to a variable
    $responseContent = $response->content();

    // Return your response
    return $response;
}