mardi 30 avril 2019

Unable to redirect to a new route by Ajax call after post method in Laravel

I am doing a search operation. I am passing 3 search parameters in the form, which makes an ajax call. I am getting the response from the ajax call, but I am unable to redirect it to the new route, with the search response data. When I try to redirect, I get the full HTML Response. Unable to figure out how to redirect it to the new page with the new search results.

I am using: Laravel 5.5 with Ajax

form.blade.php

  <form id="myform">
     <input type="text" name="city" id="city">
     <input type="text" name="locality" id="locality">
     <input type="text" name="type" id="type">
     <button id="search" value="Submit"> 
  </form> 

the ajax call i am making in the form.blade.php

  <script>
    jQuery(document).ready(function(){
        jQuery('#search').click(function(e){
        e.preventDefault();
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        jQuery.ajax({
            url: "",
            method: 'post',
            contentType: "application/json",
            data: JSON.stringify({
                type: jQuery('#type').val(),
                city: jQuery('#city').val(),
                locality: jQuery('#locality').val()                    
            }),
            success: function(result){
                jQuery('.alert').show();
                                    jQuery('.alert').html(result.success);
                                    console.log(result);
            },error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(errorThrown);
                }
            });
        });
        });
</script>

I have defined my route in api.php

Route::post('/searchlisting', 'SearchController@searchtypes');

In my SearchController i have defined the following:

public static function searchtypes(Request $request){
    $data = array(
        'city' => $request->city,
        'type' => $request->type,
        'locality' => $request->locality
    );

    $result = SearchModel::searchtypes($data);
    return view('alllistings', ['listing' => $result]);

In my SearchModel I have defined the following:

 public static function searchtypes($inp = []){
    $result = DB::table('tbl_types')
                ->where('city', $inp['city'])
                ->where('bhk', $inp['type'])
                ->where('locality', $inp['locality'])
                ->get();
    return $result;
}

In the ajax response I get the full HTML with the desired values. But how do I redirect it to the new page with the search results.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2V6Wc5n
via IFTTT

Aucun commentaire:

Enregistrer un commentaire