Laravel中的post方法后,无法通过Ajax调用重定向到新路线

时间:2019-05-01 04:53:16

标签: ajax laravel-5

我正在执行搜索操作。我以表格的形式传递了3个搜索参数,从而进行了ajax调用。我从ajax调用中获得响应,但是无法使用搜索响应数据将其重定向到新路由。当我尝试重定向时,会收到完整的HTML响应。无法弄清楚如何使用新的搜索结果将其重定向到新页面。

我正在使用:带有Ajax的Laravel 5.5

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> 

我以form.blade.php进行的ajax调用

  <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: "{{ url('/api/searchlisting') }}",
            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>

我已经在api.php中定义了我的路线

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

在我的SearchController中,我定义了以下内容:

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

在我的SearchModel中,我定义了以下内容:

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

alllistings.blade.php

<head>
<title>Get listing near you area</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum- 
scale=1">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/main-color.css" id="colors">

</head>

<body>
@include('common/header')

<div class="row">

            <!-- Listing Item -->
        @foreach($listing as $key=> $result)
            <div class="col-lg-12 col-md-12">
                <div class="listing-item-container list-layout">
                    <a href="/listings-single-page/{{$result->id}}" class="listing-item">

                        <!-- Image -->
                        <div class="listing-item-image">
                            <img src="{{$result->timgpath}}" alt="">
                            <span class="tag">{{$result->locality}}</span>
                        </div>

                        <!-- Content -->
                        <div class="listing-item-content">
                            <!-- <div class="listing-badge now-open">Now Open</div> -->

                            <div class="listing-item-inner">
                                <h3>{{$result->title}} <i class="verified-icon"></i></h3>
                                <i class="fa fa-map-marker" style="padding-right: 6px; font-size: 19px;"></i><span>{{$result->city}}</span>
                                <div class="star-rating">


                            <!-- <span class="like-icon"></span> -->


                        </div>
                    </a>
                </div>
    </div>
            @endforeach

        </div>
 </body>

在ajax响应中,我获得了具有所需值的完整HTML。但是如何将其重定向到带有搜索结果的新页面。

1 个答案:

答案 0 :(得分:2)

不要使用ajax,只需将表单代码替换为:

<form id="myform" method="post" action="{{ url('/api/searchlisting') }}">
     <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> 

并添加csrf令牌字段作为隐藏字段