Давайте перепишем весь материал
var data = $(this).serialize();
$.ajax({
url: "{{ route('ajaxdata.getactivities') }}",//if this page
// does not end with something.blade.php, it will not render your url, i.e if you have seperate .js file, consider rewriting this line, probably like so
// $url=$(this).attr('action');
type:"GET",
datatType : 'json',
data: data,
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`
},
})
Выглядит так, как будто вы хотите вернуть представление, вам нужно будет отрендерить его так:
function getactivities()
{
$activities = Activity::orderBy('id','asc')->get();
$data=view('student.ajaxdata', compact('activities'))->render();
return response()->json(['html'=>$data]);
}
и получите доступ к нему в своем успехе ajax как
console.log(data.success)// this will return the page with the value and not the values alone.
но если вы не хотите возвращать страницу, то поработайте так
function getactivities()
{
$activities = Activity::orderBy('id','asc')->get();
//return view('student.ajaxdata', compact('activities'));
return response()->json(['$data'=>$activities]);
//remember the data returned here is a collection since you are using a `get() method, you cannot do data.id in your ajax without iterating over it, if you plan to return just a row, then rewrite this line`
$activities = Activity::orderBy('id','asc')->first();
}
и доступ к нему таким же образом в ajax success.
console.log(data)
$data: Array(3)
0:
ActionDescription: ""
ActivityDate: "0000-00-00"
ActivityID: "1"
ActivityName: "Training"
ActivityTime: "00:00:00"
ActivityTypeID: ""
Location: "Moa"
QRCode: ""
created_at: null
event_id: 1
id: 1
updated_at: null
__proto__: Object
1: {id: 3, ActivityName: "Bruno", Location: "NY", ActionDescription: "A", ActivityDate: "0000-00-00", …}
2:
ActionDescription: "Training"
ActivityDate: "0000-00-00"
ActivityID: null
ActivityName: "Bad Blood"
ActivityTime: "12:00:00"
ActivityTypeID: null
Location: "SM Trinoma"
QRCode: null
created_at: "2019-04-11 05:38:30"
event_id: 2
id: 2
updated_at: "2019-04-11 05:38:30"
__proto