如何通过ajax fetch api响应将从laravel控制器接收到的数据发送到另一个页面

Posted

技术标签:

【中文标题】如何通过ajax fetch api响应将从laravel控制器接收到的数据发送到另一个页面【英文标题】:how to send data received from laravel controller to another page through ajax fetch api response 【发布时间】:2019-12-22 04:01:22 【问题描述】:

我正在尝试通过 javascript fetch API 将从控制器接收到的数据发送到另一个页面。我怎样才能传递这些数据

$(document).on('click', '#edit-property', function () 

  var data = $(this).data('info');

  fetch('/property/' + data)

    .then(response => 

      if (response.ok) 

        response.json().then(property => 

          console.log(property);

          window.location.href = "/property-details";

        )
       else 
        console.error(' Reponse serveur : ' + response.status);
      

    );
);

【问题讨论】:

【参考方案1】:

当您重定向到您将数据发送到的页面时,您可以将数据作为参数传递到您的 url。

    response.json().then(property => 

      window.location.href = "/property-details?property="+property ;

    )

然后这里是控制器方法检索附加到 url 的数据:

MyDataController.php

public function getRequestParams(Request $request)

    $property = request()->property;
    return view('display_property')->with('property', $property));

【讨论】:

以上是关于如何通过ajax fetch api响应将从laravel控制器接收到的数据发送到另一个页面的主要内容,如果未能解决你的问题,请参考以下文章