【发布时间】: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