onclick 使用 ajax laravel 从数据库中获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46019539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
onclick get data from database using ajax laravel
提问by Harish Karthick
What I am trying to do is to load data-id
in drop-down menu when I click the list item, it need retrieve the data from db and append in table.
我想要做的是data-id
在单击列表项时加载下拉菜单,它需要从 db 检索数据并附加到表中。
So far, I loaded the data id in drop-down menu and while on-click the list I send ajax call to retrieve data.
到目前为止,我在下拉菜单中加载了数据 ID,并在单击列表时发送 ajax 调用来检索数据。
My code (controller page):
我的代码(控制器页面):
public function calcList($id)
{
$designId=design::find($id);
$design = design::select( `design_no`, `design_name`, `weight_length`, `quantity`, `design_image`, `des2`, `des3`, `des4`, `des5`, `des6`, `des7`)->where('id','=',$designId);
// return Response::json(array('datas' => $design));
// return response()->json(['data'=>$design]);
return Response::json($design);
}
Ajax Request:
阿贾克斯请求:
$(document).ready(function() {
$('.designList').click(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var id =($(this).attr("data-id"));
$.ajax({
type:"POST",
url:"/calc_list/"+id,
success : function(results) {
console.log(results);
}
});
});
});
On the console log, the results print like {..}
How to built the json to table?
在控制台日志上,结果打印为 {..}
How to build the json to table?
采纳答案by KalyanLahkar
You don't need to find designId. You just need to do
您不需要查找 designId。你只需要做
public function calcList($id) {
$design = design::select( `design_no`, `design_name`, `weight_length`, `quantity`, `design_image`, `des2`, `des3`, `des4`, `des5`, `des6`, `des7`)->where('id',$id)->get();
return Response::json($design);
}
You should not make another query to database by first finding a single design with id then using it again to get design_no, design_name, ... etc. It seems redundant.
您不应该通过首先找到带有 id 的单个设计然后再次使用它来获取 design_no、design_name 等来对数据库进行另一个查询。这似乎是多余的。
回答by shukshin.ivan
First, you'd better use GET
method with cache: false
ajax setting. In this case you don't need to pass csrf
token.
首先,你最好使用GET
带有cache: false
ajax设置的方法。在这种情况下,您不需要传递csrf
令牌。
Second, you forgot to use get()
method to get data. Third, you can use where
without =
, it is by default.
其次,您忘记使用get()
方法来获取数据。第三,你可以使用where
without =
,它是默认的。
use Response; // don't forget or use response()
public function calcList($id)
{
$designId=design::find($id);
$design = design::select( 'design_no', 'design_name', 'weight_length', 'quantity', 'design_image', 'des2', 'des3', 'des4', 'des5', 'des6', 'des7')->where('id', $designId)->get();
return Response::json($design);
}
You can display your results as a table as follows.
您可以将结果显示为如下表格。
<div id="destination"></div>
...
$.ajax({
type:"POST",
url:"/calc_list/"+id,
success : function(results) {
var $table = $('<table></table>');
$('#destination').html('');
for(var i=0;i<=results.length;i++) {
$table.append('<tr><td>No</td><td>'+results[i].design_no+'</td></tr>');
$table.append('<tr><td>Name</td><td>'+results[i].design_name+'</td></tr>');
}
$('#destination').append($table);
}
});
回答by Sreejith BS
You are checking the condition the wrong way. find($id)
retrieve single RECORD, not the primary key. Modify your controller method.
您正在以错误的方式检查条件。find($id)
检索单个记录,而不是主键。修改您的控制器方法。
public function calcList($id) {
$design = design::select( `design_no`, `design_name`, `weight_length`, `quantity`, `design_image`, `des2`, `des3`, `des4`, `des5`, `des6`, `des7`)->where('id','=', $id)->get(); //*** change to id
// return Response::json(array('datas' => $design));
// return response()->json(['data'=>$design]);
return Response::json($design);
}
More: https://laravel.com/docs/5.4/eloquent#retrieving-single-models
更多:https: //laravel.com/docs/5.4/eloquent#retrieving-single-models
Also make sure you add this in your head
还要确保将其添加到您的脑海中
<meta name="_token" content="{{ csrf_token() }}" />