Is there a simpler way to do this ?
Feb 26, 2019
19
Level 1
How to format json data in laravel?
I need to get rid of json format and show only text with applied css.
Displayed data:
{"id":"<h3>...............................................<h3><br \>
<h1>rzNC35gDNG6moR3w<h1>"}
Controller:
function requete()
{
$id = Str::random();
$res = '<h3>...............................................<h3>'
."<br \><h1>".$id.'<h1>';
return response()->json(['id' => $res]);
}
View:
$(document).ready(function() {
$.ajax({
url:"{{ route('support.requete') }}",
});
});
Level 104
The solution I gave you above will absolutely work; however, you need a separate view partial to render the HTML for the AJAX request. This view partial should only contain the HTML that should be rendered into the existing view, e.g. a file called partial.blade.php inresources/views` directory:
<!-- resources/views/partial.blade.php -->
<h3>...............................................</h3>
<br \>
<h1>{{ $id }}<h1>
That's it, nothing else... no @section, no @extends etc.
You then render this as the AJAX response:
// Controller
function requete()
{
$id = Str::random();
return view('partial', compact('id'))->render();
}
Finally, the AJAX request:
$(document).ready(function() {
$.ajax({
type: 'GET',
url:"{{ route('support.requete') }}",
success: function (response) {
$('#code').append(response);
}
});
});
This will work.
Please or to participate in this conversation.