In SuiteScripts, I'm returning JSON. When I use st...
# suitescript
b
In SuiteScripts, I'm returning JSON. When I use static text it works fine.
Copy code
paramsObj = {'type':'success', 'message':'Success'}
var outPutParameters = JSON.stringify(paramsObj);
response.write(outPutParameters);
However if I try to pass html code like this
Copy code
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 appreciated
n
I'm not sure but in the absence of anyone else replying, have you tried encoding it in case that's all it is? i.e. encodeURI() ?
cheers 1
b
I don't think it's related to URL
🤷🏻‍♂️ 1
e
Copy code
var msg = '<a href="google.com">Google</a>';
msg = msg.split( ">" )
.join( ">" )
.split( "&gte;" )
.join( ">=" )
.split( "<" )
.join( "<" )
.split( "&lte;" )
.join( "<=" );
paramsObj = {'type':'success', 'message':msg}
var outPutParameters = JSON.stringify(paramsObj);
response.write(outPutParameters);
Try this ^, it's the
<
and
>
characters that it doesn't like
The replacement of
<=
and
>=
is overkill in your case- but I just happened to run into this the other day and also needed to replace those
n
cough cough
😂 2
👆 1
b
Thank you
e
@NElliott For my own knowledge- would encodeURI do what I'm doing there? (but probably with far more use cases handled?)
n
Copy code
var msg = '<a href="google.com">Google</a>';
msg = encodeURI(msg);
console.log(msg)
'%3Ca%20href=%22google.com%22%3EGoogle%3C/a%3E'
So no not really but still going to be safe...
e
Gotcha - thanks for the tip
b
Thank you.
encodeURI()
did the trick.
w
Set the response headers and you shouldn't have to do that.
b
Yes I was trying to do that but it was returning error in the client script side where send XMLHttpRequest as it is written in SS1.0
w
Is the suitelet (I assume) written in 1.0?
Copy code
context.response.setHeader({ name: 'Content-Type', value: 'application/json' })
context.response.write(jsonResults)
b
Copy code
http.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,
yes
and in server side,
Copy code
response.setHeader('Content-Type','application/json');
response.write(jsonStr)
shows me error header is not set properly in client script
w
"Content-type" vs "Content-Type"?
b
I didn't try 'Content-type'
w
Netsuite adds the additional four line with
<!-- 11,373...
to the response when it sees a < or > in the string. If you set the header, it doesn't
b
Okay
Thanks for letting me know
w
I doubt the capital T matters... 🤔
g
You can try a function function createGoogleLink() { var link = document.createElement("a"); link.href = "https://www.google.com"; link.textContent = "Google"; document.body.appendChild(link); } createGoogleLink();
b
Good point @Gregory. Thank you. That will work
g
im pretty sure I wrote it correctly, but give it a quick test first.
b
Thank you