我从laravel和ajax试用中获取数据时遇到了不确定的问题。这是我的阿贾克斯var data = $(this).serialize();$.ajax({ url: "{{ route('ajaxdata.getactivities') }}", type: "GET", datatType: 'json', data: data, cache: false, processData: false, error: function (data) { console.log('AJAX call Failed'); }, success: function (data) { console.log('AJAX call success'); $('#test').append('Add' + data.id); //data.id is showing undefined. if it is only data it doesnt show anything but AJAX call success },});这是我的路线Route::get('ajaxdata/getactivities', 'AjaxdataController@getactivities') ->name('ajaxdata.getactivities');这是我的控制器功能function getactivities(){ $activities = Activity::orderby('id', 'asc')->get(); return view('student.ajaxdata', compact('activities'));}
3 回答
倚天杖
TA贡献1828条经验 获得超3个赞
使用$ .each的所有麻烦都无法用于循环数据,因此我用于输出数据。这是我的工作代码
$(document).ready(function() {
var data = $(this).serialize();
$.ajax({ url: "{{ route('ajaxdata.getactivities') }}",
method: "get",
dataType:"JSON",
data: data,
// cache: false,
// processData: false,
success: function (data)
{
$.each(data,function(key,value) {
for(var x=0; x < value.length; x++){
console.log(value[x].ActivityName);
$('#activities').append($('<option>', {value:value[x].id, text:value[x].ActivityName}));
}
})
},
});
});
- 3 回答
- 0 关注
- 136 浏览
添加回答
举报
0/150
提交
取消