I am facing the error " org.mozilla.javascript.Ec...
# suitescript
m
I am facing the error " org.mozilla.javascript.EcmaError: SyntaxError: Unexpected token: < " but cant find the solution can anyone help please
l
Usually this error is when your script is trying to parse a string (JSON) into an object but this string is malformed
👍 1
c
Share your code
m
ok sure
var count=0; for(j=0;j<75;j++) { var param = 20*j; var result = https.requestRestlet({ body: JSON.stringify(param), deploymentId: 'customdeploy1', headers: { "Content-Type": "application/json" }, method: 'POST', scriptId: 643 }); var body=JSON.parse(result.body); var items=body.items; for(item in items) { list.addRow({ row : { internal_id : items[item].internal_id , name : items[item].name , display_name : items[item].displayName, type : items[item].type, quantity : items[item].quantity , display_website : items[item].displayWebsite } }); content+=items[item].internal_id+','+items[item].name.replace(/,/g,' ')+','+items[item].displayName.replace(/,/g,' ')+','+items[item].type+','+items[item].quantity+','+items[item].displayWebsite+'\n'; count++; } } log.debug('Count is ',count);
this is not the whole code just a little snipped , I think the problem is in this line var body=JSON.parse(result.body);
l
log the result.body before parse so you can check if it’s a JSON… also, check if the response code is 200 cause if it’s return an error the body is empty or a plan text
👍 1
j
"Unexpected token: <" usually occurs when you try to parse HTML or XML to JSON. i.e. the first character JSON.parse encounters is an angle bracket. e.g. <html> If you log the result.body as suggested by Luiz you will probably find that the result is being returned as HTML not JSON.
👍 1