I'm also looking for an answer for this question
Feb 25, 2019
6
Level 1
How to pass data to view?
I need to pass data from controller to view this is my code :
Controller :
function requete()
{
$id = Str::random();
echo json_encode($id);
return view('Demo.requete', compact('id'));
}
View :
$(document).ready(function() {
$.ajax({
url: "{{ route('support.requete') }}",
method: "get",
data: data,
dataType: "json",
success: function(data)
{
$('#code').html(data.id);//nothing happens here
}
});
});
The data is displaying on top of the page not in the '#code' section and i get this error :
jquery.min.js:2 jQuery.Deferred exception: data is not defined ReferenceError: data is not defined
at HTMLDocument.<anonymous> (http://127.0.0.1:8000/support/requete:34:26)
at j (https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:2:29568)
at k (https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:2:29882) undefined
r.Deferred.exceptionHook @ jquery.min.js:2
k @ jquery.min.js:2
setTimeout (async)
(anonymous) @ jquery.min.js:2
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
fire @ jquery.min.js:2
i @ jquery.min.js:2
fireWith @ jquery.min.js:2
ready @ jquery.min.js:2
R @ jquery.min.js:2
jquery.min.js:2 Uncaught ReferenceError: data is not defined
at HTMLDocument.<anonymous> (requete:34)
at j (jquery.min.js:2)
at k (jquery.min.js:2)
Is there a simpler way to do this?
Please or to participate in this conversation.