我是新來的Ajax,並希望返回laravel中的部分視圖以響應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));
}
這是我項目,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
感謝您的回覆,我試過了,但仍然是相同的錯誤 – Khodadad
好的 - 你得到了什麼錯誤?你可以console.log(結果)並檢查裏面是什麼? –
控制檯中的錯誤:jquery-1.11.2.min.js:4 POST http:// localhost:8000/sales/bill 500(內部服務器錯誤)和Laravel Debugbar:說 - >嘗試獲取非屬性-dject從我的部分頁面 – Khodadad