I'm using laminas-escaper for escaping values in html and javascript. I fetch values from the server using ajax. So I escape them in php using laminas-escaper. When i fetch the data from server I store them in a js variable and sometimes even display them using html. For example in sample.js:
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "process.php", true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.onreadystatechange=function()
{
if (xhttp.readyState == 4 && xhttp.status == 200)
{
var response = JSON.parse(xhttp.responseText);
var name = response.name;
document.getElementById('demo').innerHTML = name;
}
}
xhttp.send();
The response came from the server will be in json format. For example : '{name:xxx,address:yyy}'
My question is should i only use $escaper->escapeJs($input) for escaping js, or only use $escaper->escapeHtml($input) for html escaping or use both. If I have to use both then in which order.
Note: I perform escaping in server and send the result to the browser.
will be in json format. For example... example is not JSON - why have you shown javascript code that retrieves JSON data, then the question is about escaping HTML or JS? you would use neither for JSON data - as JSON is JSON, HTML is HTML and JS is JSjson_encode($content)and that's it, no need to additionally escape anything