How do I end a server script early without running to the end of the file?
For example:
codeif(...) {
response.error("POST JSON parse error: " + e, 'Bad Request');
return;
} else {
...
}/code
How do I end a server script early without running to the end of the file?
For example:
codeif(...) {
response.error("POST JSON parse error: " + e, 'Bad Request');
return;
} else {
...
}/code
Hi Jason,
You can do it with following line of JS code:
pre
response.success("Some response content.");
/pre
Regards.
But it seems when I have a situation like:
pre
response.error("Some response content #1.");
...
response.success("Some response content #2.");
...
response.error("Some response content #3.");
/pre
Only "content #3" is returned. I'm looking for a way to end the script immediately when I want to return a error.
Right now I have a lot of
pre
if (!abort) {
...
}
...
if (!abort) {
...
}
...
/pre
And I'm looking for a cleaner way to code the scripts.
Hello Jason,
Please try using return after your if clause.
Also please try to write proper javascript which exclude several conditions to have 'true' value.
When I try I get this error:
preScript test: SyntaxError: Illegal return statement ( @ 3 : 2 ) - return;/pre
My test script was:
preif (request.get('val') == 'yes') {
response.success("End #1", "text/plain");
return;
}
response.success("End #2", "text/plain");/pre
Hi Jason,
"return" statement could be used only withing the function.
Thus please try to wrap all your code with some function and call this function:
pre
var InvokeServiceFunction(){
Code: Select all
//Here is all your needed JS code. Here you can use "return" statement.
};
InvokeServiceFunction();
/pre
Regards.
Oh! That makes sense. I'll give that a try.