Ajax搜索数据库并不是加载结果。 Laravel

时间:2018-02-21 13:08:21

标签: jquery ajax laravel

我有一张表格。在表单的一个输入中,我尝试实现动态预先输入功能。

我得到的错误是

  

无法加载资源:net :: ERR_BLOCKED_BY_CLIENT   advertise-ajax?term = b& _type = query& q = b无法加载资源:

表单本身的路径,然后是ajax

Route::get('/advertise', 'AdvertisementController@index');
Route::get('/advertise-ajax', 'AdvertisementController@townload');

以上的控制器是

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\PropertyAdvert;
use DB;
use Auth;

class AdvertisementController extends Controller
{
    public function index(){
      return view('pages/Advert/create');
    }

    public function store(Request $request){
      $Advert = new PropertyAdvert();

      $Advert::create(
        [
          "photo"       => $request->photo,
          "address"     => $request->address,
          "county"      => $request->county,
          "town"        => $request->town,
          "type"        => $request->type,
          "rent"        => $request->rent,
          "date"        => $request->date,
          "bedrooms"    => $request->bedrooms,
          "bathrooms"   => $request->bathrooms,
          "furnished"   => $request->furnished,
          "description" => $request->description
        ]
      );

      return "Success. Your adveret has been published";
    }

    public function show($id){
    $Advert = PropertyAdvert::where('id', $id)->first();
    return view('pages/Advert/show', compact('Advert'));
  }

  public function townload(Request $request)
    {
        $data = [];


        if($request->has('q')){
            $search = $request->q;
            $data = DB::table("town")
                    ->select("id","name")
                    ->where('name','LIKE',"%$search%")
                    ->get();
        }


        return response()->json($data);
    }

}

与表单位于同一刀片模板中的ajax。 这是表单中的一个输入。

我已将城镇存储在名为town的数据库中。如果用户键入dub,则dublin应作为选项出现,例如。

<select class="town form-control" style="width:500px;" name="town"></select>

                        <script type="text/javascript">


      $('.town').select2({
        placeholder: 'Select an item',
        ajax: {
          url: '/advertise-ajax',
          dataType: 'json',
          delay: 250,
          processResults: function (data) {
            return {
              results:  $.map(data, function (item) {
                    return {
                        text: item.name,
                        id: item.id
                    }
                })
            };
          },
          cache: true
        }
      });


</script>

2 个答案:

答案 0 :(得分:0)

where功能的参数错误!这应该是这样的:

->where('name','LIKE',"%".$search."%")

答案 1 :(得分:0)

  

由于您的某些广告拦截器阻止,您获得了ERR_BLOCKED_BY_CLIENT   你的要求

如果您的网址包含“广告”,“点击”,“广告”,“双击等字词 STRONG>” 然后ad-blocker会阻止它。

您有 advertise-ajax 网址,因此广告拦截器认为这是针对广告的,因此它阻止了您的请求,因此您可以为您的网址使用其他字词