I am writing an angularjs app which is consuming an api built with Laravel 4.1. I am looking to protect against json injection.
One method built into angularjs to fix this is to prefix all server json responses with the following string ")]}',\n"
.
The angularjs $http service will automatically strip this string from all json responses.
I don't want to have to attach this string manually to every json response which my api serves.
Is there a way to prefix this string whenever my controller returns a json Response object?
return Response::json($prefix.$json, 200);
I know this question was asked a long time ago, but to help who needs this approach with Laravel 5 and AngularJS this can be done by creating a Response Macro, so in your
ResponseMacroServiceProvider
class you'd defineboot
method like:Then simply call the
response()
helper function within your controller:This should work:
If you want to prepend/append data to the response you can use filters.
You can then attach the filter to the route using the
after
property.Alternatively, if you don't want to attach a filter to each route that outputs json, then it is also possible to utilise the
App::after
hook.