1

I'm using Laravel 5 and I'm doing some filters on some collections in my controller. The problem is using AJAX to be the bridge between the blade template and Controller. Here is my jQuery code:

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. But it's returning a 404 code, Not Found. Any idea what I'm doing wrong?

6 Answers 6

3

Add this to your ajax call

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

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);
      }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I did, but still nothing. I updated the post to correct the typo in the route and to add this in the request. Still 404 error
2

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.

public function search(Request $request)

and after that access to the $txt variable like this

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

);

and in jQuery code use this:

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

3 Comments

You're right, but it still giving me a 404 not found.
try to load the url from browser, if is okay, try it from the ajax form and from the developers tools of the browser, check the response. Give us the new Route() code and the url that you write into the adress bar.
Now it's calling the method if I put the url directly in the browser. http://localhost/app/public/search/hello
0

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);
  }
});

2 Comments

ROUTE must be of type get
And it is. Route::get('/search/{$txt}', 'PrestashopController@search');
0

try with the full-Path URL..

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

let your AJAX be like this..

 $.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);
          }
 });

2 Comments

check the requst URL in console, or is there any error in console ..?
Here is the general info in the console: Request URL:http://localhost/a3p/public/search?txt=ddd Request Method:GET Status Code:404 Not Found Remote Address:[::1]:80
0

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
}

6 Comments

Can plz do one thing for me.. go to browser and type your url e.g app.dev/search/mystring.. and tell me your method is called or not
It's saying the route doesn't exist. But here is my route: Route::get('/search/{$txt}', 'PrestashopController@search');
Update this Route::get('/search/{txt}', 'PrestashopController@search');
Route::get('/search/{txt}', 'PrestashopController@getSearch'); public function getSearch($searchText){ dd($searchText); return "results"; //Just to see if it's returning correctly }
Now check.. I m updating my answer
|
0

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
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.