Boris Yasen
11/08/2023, 2:42 PMparamsObj = {'type':'success', 'message':'Success'}
var outPutParameters = JSON.stringify(paramsObj);
response.write(outPutParameters);
However if I try to pass html code like this
paramsObj = {'type':'success', 'message':'<a href="google.com">Google</a>'}
var outPutParameters = JSON.stringify(paramsObj);
response.write(outPutParameters);
It throws me the error in console that says.
Unexpected non-whitespace character after JSON at
So if I check its response in Networking tab I can see a line break is added after closing 'a' tag.
Any help would be appreciatedNElliott
11/08/2023, 2:57 PMBoris Yasen
11/08/2023, 3:21 PMericbirdsall
11/08/2023, 3:32 PMvar msg = '<a href="google.com">Google</a>';
msg = msg.split( ">" )
.join( ">" )
.split( ">e;" )
.join( ">=" )
.split( "<" )
.join( "<" )
.split( "<e;" )
.join( "<=" );
paramsObj = {'type':'success', 'message':msg}
var outPutParameters = JSON.stringify(paramsObj);
response.write(outPutParameters);
ericbirdsall
11/08/2023, 3:32 PM<
and >
characters that it doesn't likeericbirdsall
11/08/2023, 3:34 PM<=
and >=
is overkill in your case- but I just happened to run into this the other day and also needed to replace thoseNElliott
11/08/2023, 3:38 PMBoris Yasen
11/08/2023, 3:39 PMericbirdsall
11/08/2023, 3:41 PMNElliott
11/08/2023, 3:43 PMvar msg = '<a href="google.com">Google</a>';
msg = encodeURI(msg);
console.log(msg)
'%3Ca%20href=%22google.com%22%3EGoogle%3C/a%3E'NElliott
11/08/2023, 3:44 PMericbirdsall
11/08/2023, 3:47 PMBoris Yasen
11/08/2023, 3:54 PMencodeURI()
did the trick.Watz
11/08/2023, 4:23 PMBoris Yasen
11/08/2023, 4:24 PMWatz
11/08/2023, 4:26 PMcontext.response.setHeader({ name: 'Content-Type', value: 'application/json' })
context.response.write(jsonResults)
Boris Yasen
11/08/2023, 4:26 PMhttp.open("POST", scriptURL, async);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.send(params);
This is the client script code,Boris Yasen
11/08/2023, 4:26 PMBoris Yasen
11/08/2023, 4:27 PMresponse.setHeader('Content-Type','application/json');
response.write(jsonStr)
shows me error header is not set properly in client scriptWatz
11/08/2023, 4:28 PMBoris Yasen
11/08/2023, 4:29 PMWatz
11/08/2023, 4:30 PM<!-- 11,373...
to the response when it sees a < or > in the string.
If you set the header, it doesn'tBoris Yasen
11/08/2023, 4:30 PMBoris Yasen
11/08/2023, 4:30 PMWatz
11/08/2023, 4:31 PMGregory
11/08/2023, 6:09 PMBoris Yasen
11/08/2023, 6:10 PMGregory
11/08/2023, 6:11 PMBoris Yasen
11/08/2023, 6:12 PM