我是Ajax的新手,并希望在响应ajax调用时返回laravel中的部分视图,因此当我单击链接以模式显示数据时,它不起作用并且说试图获得非对象的属性。有什么帮助吗?
这是我的代码:
<tr>
<td style="border-right: 1px solid #ddd;"><?php echo $i;?></td>
<td>{{ $sale->item_name }}</td>
<td>{{ $sale->brand_name }}</td>
<td>{{ $sale->category_name }}</td>
<td>{{ $sale->fname }} {{ $sale->lname }}</td>
<td>{{ $sale->sale_date }}</td>
<td>{{ $sale->quantity }}</td>
<td>{{ $sale->unit_name }}</td>
<td><span>$</span>{{ $sale->unit_price }} </td>
<td><span>$</span><?php echo $sale->quantity*$sale->unit_price;?> </td>
<td>
<a href="#bill" class="btn btn-xs btn-green mr-5" type="button" tabindex="0" data-toggle="modal" data-target="#reportModal" onclick="load_modal_data('{{ $sale->sale_id }}','/sales/bill','billContent')">
<i class="fa fa-pencil"> Bill</i></a>
<a href="/purchase/item" class="btn btn-xs btn-green mr-5"><i class="fa fa-search"> View</i></a>
<a href="#" class="btn btn-xs btn-lightred"><i class="fa fa-remove"> Del</i></a>
</td>
</tr>
JS和Ajax
function load_modal_data(identity, route,target_tag)
{
$.ajax({
headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') },
url: route,
type:'post',
data:{ id: identity},
success:function(result){
console.log(result)
$('#'+target_tag).html(result);
}
})
}
控制器:
public function bill()
{
$id = Input::get('identity');
$sales = DB::table('sales')
->join('brands','brands.bid','=','sales.brand_id')
->join('units','units.unit_id','=','sales.unit_id')
->join('categories','categories.cat_id', '=','sales.category_id')
->join('customers','customers.cid','=','sales.c_id')
->select('sales.*','brands.brand_name','categories.category_name','customers.fname','customers.lname','units.unit_name')
->where('sales.sale_id',$id)->first();
$returnHTML = view('partials.item-bill')->with('sales',$sales)->render();
return response()->json(array('success'=>true, 'html'=>$returnHTML));
}
这是我的 item-bill.blade.php
<!-- tile body -->
<div class="tile-body p-0">
<div class="table-responsive">
<table class="table mb-0" id="usersList">
<thead>
<tr>
<th style="width:20px;">No</th>
<th>Item Name</th>
<th>Company</th>
<th>Category</th>
<th>Customer</th>
<th>Sale Date</th>
<th>Quantity</th>
<th>Unit</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ $sales->item_name }}</td>
<td>{{ $sales->brand_name }}</td>
<td>{{ $sales->category_name }}</td>
<td>{{ $sales->fname }} {{ $sales->lname }}</td>
<td>{{ $sales->sale_date }}</td>
<td>{{ $sales->quantity }}</td>
<td>{{ $sales->unit_name }}</td>
<td><span>$</span>{{ $sales->unit_price }} </td>
<td><span>$</span><?php echo $sales->quantity*$sales->unit_price;?> </td>
</tr>
</tbody>
</table>
</div>
注意:我使用的是Laravel 5.2
答案 0 :(得分:0)
只有你需要改变的地方就在这里:
$('#'+target_tag).html(result.html);
你得到一个json作为result.html
;
答案 1 :(得分:0)
最后我解决了这个问题并想在这里分享。 我做了以下步骤:
将路线方法从 GET 更改为 POST
Route::post('sales/bill/{id}','SalesController@bill');
更改js的参数功能load_modal_data()
<a href="#bill" ... onclick="load_modal_data('/sales/bill/{{ $sale->sale_id }}','billContent')">
也改变了js文件中的上述功能:
function load_modal_data(route,target_tag)
{
var identity = route.substring(route.lastIndexOf('/') + 1);
// $('#'+target_tag).addClass('animation-loading')
$.ajax({
headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') },
url: route,
type:'post',
data:{ id: identity},
success:function(result){
$('#'+target_tag).html(result.html);
},
error: function (result) {
alert('Ooops something went wrong!');
}
});
}
还有控制器,
public function bill($id)
{
.
.
.
}
现在它正在运作。