【问题标题】:Ajax response is not coming on View PageAjax 响应未出现在查看页面上
【发布时间】:2016-05-31 18:40:51
【问题描述】:

我的 ajax 工作正常,我试图在我的视图页面上返回 ajax 响应,但它没有返回任何内容,但在警报中我可以看到 ajax 生成的响应,所以请建议我如何将 ajax 响应返回到我的页面。

阿贾克斯:

$('.click').click(function(evt) {
    evt.preventDefault();
    var link = $(this).attr('id');
    $.ajax({
        type: "GET",
        url: '\p_details?id'+link,
        dataType: "json",

        data: {
            id: link   
        }

    }).done(function(data) { 
        $('#property').html(data.html);
        alert(data.html);
    });

});

控制器:

public function index()
{
    //$filter=Input::get('id');
    //var_dump($term);
    $view=DB::table('property_details')
        ->Where('sale_or_rent', '=', 'rent')
        ->orWhere('sale_or_rent', '=', 'sale')
        ->get();
    //  var_dump($view);
    return view::make('index', array('val'=>$view));
}

public function getPropertyDetails()
{
    $filter = Input::get('id');
    $display = DB::table('property_details')
        ->where('sale_or_rent', 'LIKE', '%' . $filter . '%')
        ->get();
    //var_dump($display); 

    if(count($display)!=0)
    {
        $returnHTML = view('/pages/fliter')->with('val', $display)->render();
        return response()->json(array('success' => true, 'html'=>$returnHTML));
    }
    else
    {
        session::flash('status', 'No Records Found!!!');
        $returnHTML = view('/pages/fliter')->with('val', $display)->render();
        return response()->json(array('success' => true, 'html'=>$returnHTML));
    } 
} 

html:

<div class="container">
    <div class="block-content block-content-small-padding">
        <div class="block-content-inner">
            <h2 class="center">Recent Properties</h2>
            <ul class="properties-filter">
                <li class="selected"><a href="#" data-filter="*" ><span>All</span></a></li>
                <li><a href="{{URL::to('\p_details?id=featured')}}" id="featured" data-filter=".property-featured" class="click"><span>Featured</span></a></li>
                <li><a href="{{ URL::to('\p_details?id=rent')}}" id="rent" data-filter=".property-rent" class="click"><span>Rent</span></a></li>
                <li><a href="{{ URL::to('\p_details?id=sale')}}" id="sale" data-filter=".property-sale" class="click"><span>Sale</span></a></li>
            </ul>


            <div class="properties-items isotope" style="position: relative; overflow: hidden; height: 810px;">
                <div class="row property">
                    @foreach($val as $value)
                    <div class="property-item  col-sm-6 col-md-3 isotope-item " style="position: absolute; left: 0px; top: 0px; transform: translate3d(0px, 0px, 0px);">
                        <div class="property-box">
                            <div class="property-box-inner">
                                <h3 class="property-box-title"><a href="#">{{$value->city}}</a></h3>
                                <h4 class="property-box-subtitle"><a href="#">{{$value->state}}</a></h4>
                                <div class="property-box-picture">
                                    <div class="property-box-price">{{$value->property_price}}</div>
                                    <div class="">
                                        <a href="#" class="property-box-picture-target">
                                            <img src="images/test/{{$value->image}}" alt="">
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
</div>

【问题讨论】:

  • 很抱歉,恐怕我不明白你的问题。在您的控制器中,您似乎正在返回一些 HTML,而在您的 JavaScript 代码中,您是 alert()-ing。那么究竟是什么出乎意料呢?你可以edit你的问题。请尽量写出清晰的问题描述。仅仅放在标题中的一行是不够的。
  • 感谢您的回复,我已经编辑了我的问题,请提出一些建议
  • “返回 ajax 响应”是什么意思? alert(data.html) 正在工作,那么您的意思是 $('#property').html(data.html) 不工作吗?您通常不会“返回”ajax 响应。您发出 ajax 请求,当它完成时,您通过回调执行一些操作(这就是您在这里使用 .done(function(data) { ...} 所做的事情)。
  • 那么我怎样才能返回对我的视图页的响应,我有一个带有 id 属性的 div,所以我试图在那个 div $('#property').html(data.html) 中返回我的响应;
  • 是的,所以$('#property').html(data.html) 什么都不做?您没有在页面中看到任何更改?该代码看起来是正确的,但我不能确定,因为我看不到 HTML 页面或 data.html 的内容;-)

标签: javascript ajax laravel laravel-5 onclick


【解决方案1】:

我已经编写了这样的 sql 查询 2 foreach 循环工作正常。 我没有在这个函数中使用任何 ajax 调用。

Html   
            <div class="block-content-inner">
                    <h2 class="center">Recent Properties</h2>
                    <ul class="properties-filter">
                        <li class="selected"><a href="#" id="All" data-filter="*" class="click"><span>All</span></a></li>
                        <li><a href="#" id="featured" data-filter=".property-featured" class="click"><span>Featured</span></a></li>
                        <li><a href="#" id="rent" data-filter=".property-rent" class="click"><span>Rent</span></a></li>
                        <li><a href="#" id="sale" data-filter=".property-sale" class="click "><span>Sale</span></a></li>
                    </ul>
                    <div class="properties-items isotope"  style="position: relative; overflow: hidden; height: 810px;">
                        <div class="row ">
                            @foreach($rent as $value)
                                <div class="property-item property-rent  col-sm-6 col-md-3 isotope-item" style="position: absolute; left: 0px; top: 0px; transform: translate3d(0px, 0px, 0px);">
                                    <div class="property-box">
                                        <div class="property-box-inner">
                                            <h3 class="property-box-title"><a href="#">{{$value->city}}</a></h3>
                                            <h4 class="property-box-subtitle"><a href="#">{{$value->state}}</a></h4>
                                            <div class="property-box-picture">
                                                <div class="property-box-price">{{$value->property_price}}</div>
                                                <div class="">
                                                    <a href="#" class="property-box-picture-target">
                                                        <img src="uploads/{{$value->image}}" alt="" style="width: 275px;height: 200px;">
                                                    </a>
                                                </div>
                                             </div>
                                        </div>
                                    </div>
                                </div>
                               @endforeach
                               @foreach($sale as $val)
                                <div class="property-item property-sale  col-sm-6 col-md-3 isotope-item" style="position: absolute; left: 0px; top: 0px; transform: translate3d(0px, 0px, 0px);">
                                    <div class="property-box">
                                        <div class="property-box-inner">
                                            <h3 class="property-box-title"><a href="#">{{$val->city}}</a></h3>
                                            <h4 class="property-box-subtitle"><a href="#">{{$val->state}}</a></h4>
                                            <div class="property-box-picture">
                                                <div class="property-box-price">{{$val->property_price}}</div>
                                                <div class="">
                                                    <a href="#" class="property-box-picture-target">
                                                        <img src="uploads/{{$val->image}}" alt="" style="width: 275px;height: 200px;">
                                                    </a>
                                                </div>
                                             </div>
                                        </div>
                                    </div>
                                </div>
                               @endforeach




                        </div>
                    </div>
                </div>

sql查询:

    public function index()
{


    $rent=DB::table('property_details')
    ->Where('sale_or_rent', '=', 'rent')
    ->take(8)
    ->get();

    $sale=DB::table('property_details')
    ->Where('sale_or_rent', '=', 'sale')
    ->take(8)
    ->get();


    $data=array('rent'=>$rent, 'sale'=>$sale);
    return View::make('index')->with($data);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-14
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-26
    • 2016-12-11
    • 2019-07-15
    相关资源
    最近更新 更多