简体   繁体   English

Laravel 5使用AJAX的jQuery

[英]Laravel 5 jQuery Using AJAX

I'm using Laravel 5 and I'm doing some filters on some collections in my controller. 我正在使用Laravel 5,并且正在对控制器中的某些集合进行一些过滤。 The problem is using AJAX to be the bridge between the blade template and Controller. 问题是使用AJAX作为刀片模板和控制器之间的桥梁。 Here is my jQuery code: 这是我的jQuery代码:

function listCreatedBy(str) {
        $.ajax({
          headers : {
             'csrftoken' : '{{ csrf_token() }}'
          },
          url: '{{ url("search") }}',
          type: "get", //send it through get method
          data:{txt:str},
          success: function(response) {
            console.log("ola");
            $('#results').html(response);
          },
          error: function(xhr) {
            console.log(xhr);
          }
        });
    }

Here is my route: 这是我的路线:

Route::get('/search/{txt}', 'PrestashopController@search')->name('search');

And here is my method in the controller: 这是我在控制器中的方法:

public function search($searchText){
    var_dump($searchText);

    return "results"; //Just to see if it's returning correctly
}

This method is empty for now because I only want to see if I can complete the ajax code first. 该方法暂时为空,因为我只想看看是否可以先完成Ajax代码。 But it's returning a 404 code, Not Found. 但它返回的是404代码,未找到。 Any idea what I'm doing wrong? 知道我在做什么错吗?

Add this to your ajax call 将此添加到您的ajax电话

headers : {
    'csrftoken' : '{{ csrf_token() }}'
}

edit 编辑

i see you use the route 我看到你使用路线

Route::("/search/{txt}" ...

Witch corresponds to 女巫对应

http://example.com/search/random%20text

What is probably happening is that you're using the route wrong the ajax call you're making will create an uri like this 可能发生的情况是您使用了错误的路由,正在执行的ajax调用将创建一个这样的uri

http://example.com/search/?txt=some%20text

try this 尝试这个

$.ajax({
      headers : {
         'csrftoken' : '{{ csrf_token() }}'
      },
      url: "{{ url("search") }}/" + encodeURIComponent(str),
      type: "get", //send it through get method
      success: function(response) {
        console.log("ola");
        $('#results').html(response);
      },
      error: function(xhr) {
        console.log(xhr);
      }
});

I think you have typo error. 我认为您输入错误。

Route::get('/search/{$txt}'

try without $ 尝试不带$

Route::get('/search/{txt}',...

One more..try to use in the controller method this code in the scopes, not $searchtext. 还有一个..尝试在控制器方法中使用该代码,而不是$ searchtext。

public function search(Request $request)

and after that access to the $txt variable like this 然后像这样访问$ txt变量

$test = $request->get('txt');
var_dump($test

); );

and in jQuery code use this: 在jQuery代码中使用以下代码:

function listCreatedBy(str) {
       var query_url = '{{ url("search") }}' + str;
        $.ajax({
          url: query_url ,
          type: "get", //send it through get method
          success: function(response) {
            console.log("ola");
            $('#results').html(response);
          },
          error: function(xhr) {
            console.log(xhr);
          }
        });
    }

NOTE: csrftoken is for submit forms 注意: csrftoken用于提交表单

Try to do like this 尝试这样做

$.ajax({
  url: "/search/"+str,
  type: "GET",
  data:'',
  success: function(response) {
    document.getElementById("results").innerHTML = this.responseText;
  },
  error: function(xhr) {
    console.log(xhr);
  }
});

Route::get('/search/{txt}', 'PrestashopController@getSearch');

public function getSearch($searchText){
  dd($searchText);
  return "results"; //Just to see if it's returning correctly
}

Try this: 尝试这个:

$.ajax({
  url: '{{ url("/route_name") }}',  
  // here route_name is a named route that call a controller function
  type: "get", //send it through get method
  data:{txt:str},
  success: function(response) {
    $('#results').html(response);
  },
  error: function(xhr) {
    console.log(xhr);
  }
});

try with the full-Path URL.. 尝试使用完整路径的URL。

 url: '{{ url("/search") }}',  

let your AJAX be like this.. 让您的AJAX像这样。

 $.ajax({
          url: '{{ url("/search") }}',
          type: "get", //send it through get method
          data:{txt:str},
          success: function(response) {
            document.getElementById("results").innerHTML = this.responseText;
          },
          error: function(xhr) {
            console.log(xhr);
          }
 });

You can try this one; 您可以尝试这个;

  $.ajax({
      url: "/search/",
      type: "GET",
      data:{txt:str},
      success: function(response) {
        document.getElementById("results").innerHTML = this.responseText;
      },
      error: function(xhr) {
        console.log(xhr);
      }
    });

Route: 路线:

Route::get('/search/', 'PrestashopController@getSearch');

Controller: 控制器:

public function getSearch(Request $request){
  dd($request->txt);
  return "results"; //Just to see if it's returning correctly
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM