weixin_33739541 2018-04-14 05:48 采纳率: 0%
浏览 57

Laravel 5.6使用Ajax

I am trying to use AJAX to get sorted products off of a database. However AJAX keeps sending this error.

exception: "ErrorException" ​​ file: "C:\xampp\htdocs\WebProject\storage\framework\views\c39cbea2eafe7197b8efb68251988b2c5b3a6834.php" ​​ line: 5 ​​ message: "Trying to get property 'URL' of non-object (View: C:\xampp\htdocs\WebProjectesources\views\producto_indiv.blade.php)"

The thing is that the view producto_indiv has no connection to the javascript code for ajax. The view where the error comes from shows no errors when being accessed.

This is the producto_indiv, where the error is supposed to come from, however this view is unrelated to the one with the ajax code

<div class="contenedor" id="Producto_Indiv_Page">
  <div class="item-cont">
    <?php $url = "/storage/images/products/" . basename($producto->URL); ?>
    <img src="{{$url}}">
    <div class="line"></div>
    <div class="traits">
      <div><span class="name">{{$producto->nombre_prod}}</span></div>
      <span>Precio: ${{$producto->precio}}</span>
      <span>Descripcion: {{$producto->descripcion}}</span>
      <span>marca: {{$producto->marca}}</span>
      <span>tamanos: {{$producto->tamanos}}</span>
    </div>
  </div>
</div>

AJAX code

$('.sort_opt').click(function(e) {
  e.preventDefault();
  jQuery.ajaxSetup({
    headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
  });
  jQuery.ajax({
    dataType: 'json',
    type: 'GET',
    url: '/ropa/sort',
    data: {
      type: this.name,
      genero: this.gender,
    },
    success: function(result) {
      console.log("DONE");
    },
    error: function(result) {
      console.log(result);
    }
  });
});

and controller

  public function sort(Request $request) {
    $producto = DB::table('productos')->where('$request->genero')->get();
    switch($request->type) {
      case 'nombre':
        $producto->orderBy('nombre_prod', 'asc');
        break;
      case 'marca':
        $producto->orderBy('marca', 'asc');
        break;
      case 'precio_desc':
        $producto->orderBy('precio', 'desc');
        break;
      case 'precio_desc':
        $producto->orderBy('precio', 'desc');
        break;
      case 'new':
        $producto->latest();
        break;
      case 'old':
        $producto->oldest();
        break;
    }

      return \Response::json($producto);
  }

I have been stuck on this before but changed to using redirects instead of ajax, however in this I cannot do that due to the fact that I am trying to sort a grid of products, sent from the DB.

  • 写回答

2条回答 默认 最新

  • csdn产品小助手 2018-04-14 06:01
    关注

    This problem has arrived because your $producto object has no Url, so please check first the $producto->URL and then pass the URL as below way.

    <?php 
    $productURl = "";
    if(isset($producto->URL)) {
      $productURl = $producto->URL;
    }
    
    ?>
    
    <div class="contenedor" id="Producto_Indiv_Page">
      <div class="item-cont">
        <?php $url = "/storage/images/products/" . basename($productURl); ?>
        <img src="{{$url}}">
        <div class="line"></div>
        <div class="traits">
          <div><span class="name">{{$producto->nombre_prod}}</span></div>
          <span>Precio: ${{$producto->precio}}</span>
          <span>Descripcion: {{$producto->descripcion}}</span>
          <span>marca: {{$producto->marca}}</span>
          <span>tamanos: {{$producto->tamanos}}</span>
        </div>
      </div>
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本