duanbi2003 2019-01-06 12:14
浏览 53

在ajax成功之后,Laravel将数据附加到$ cList

I dont know how to append new data from controller to the html view.

my ajax post (it works):

   $.ajax({
    type: 'get',
    dataType: "json",
    url:getWizardSelectionUri,
    data: ....
    success:function(data){ 
        $('#bikeliste').empty()
         .....

here is my html with the static request

 @foreach(App\Bike::all() as $cList)
    <li class="list-group-item">
        <table border="0" width="100%">
          <colgroup width="200" span="3"></colgroup>
            <td>   
                <a href="{{$cList->link}}" target="_blank">

                    <img src="{{$cList->bildlink}}" height="150" width= "auto"> 
                </a>
                <br>
            </td>
            <td>
                <h5>
                    <a href="{{$cList->link}}" target="_blank">
                        <strong>{{$cList->hersteller}} </strong>
                        {{$cList->modell}} 
                        {{$cList->modelljahr}} 
                        <br>

                    </a>
                </h5>
            </td>
            <td>
                @if ($cList->gewicht)
                    <strong>Gewicht: </strong> {{$cList->gewicht}}<br>
                @endif
                @if ($cList->laufraddurchmesser)
                    <strong>Radgröße: </strong>{{$cList->laufraddurchmesser}}<br>
                @endif


            </td>
        </table>
        <br>
    </li>
@endforeach

is there a posibility to change the data without reloading the page?

  • 写回答

1条回答 默认 最新

  • doushan7077 2019-01-06 12:21
    关注

    You are sending data from your ajax to your Controller asynchronously, so in your controller method you just return a json response:

    public function store(Request $request) {
        ... your store function
    
        if($store) {
            return response()->json([
                'data' => $data
            ]);
        }
        return response()->json([
            'data' => $data
        ]);
    
    }
    

    $data contains the stuff your saved in your database you return that data to your ajax promise and get the data. So in your ajax success function you have to do something like this:

    success:function(response){ 
        ...
        var data = response.data // holds the data
        yourhtml.append(data); // dissables this one ...
    

    In your success you now holds the saved data and need your append that now into your html.

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)