I try to export a json file from my controller by ajax, but nothing happens. Anyone can say me where i failed ?
Here my ajax :
$.ajax({
url: '{{ path('export_index', {'type': 'json'}) }}',
type: 'POST',
data: {data: data}
}).done(function() {
});
And here my route controller :
public function export(Request $request):Response {
header('Content-Type: application/json');
header('Content-Disposition: attachment; filename=data.json');
header('Expires: 0'); //No caching allowed
header('Cache-Control: must-revalidate');
header('Content-Length: ' . strlen($mydata));
file_put_contents('php://output', $mydata);
return new Response();
}
CodePudding user response:
Use proper syntax $.ajax
const base_url = 'myurl/';
$.ajax({
type: "GET",
// url: "https://jsonplaceholder.typicode.com/posts",
url: base_url "export_index?type=excel",
contentType: "application/json",
dataType: "json",
//data: JSON.stringify({data: data}),
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
},
done: function() {
console.log("done");
}
});
const base_url = 'myurl/';
$.ajax({
type: "get",
url: "https://jsonplaceholder.typicode.com/posts",
//url: base_url "export_index?type=excel",
contentType: "application/json",
dataType: "json",
//data: JSON.stringify({data: data}),
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
},
done: function() {
console.log("done");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>