Help me please! 🙁
How can I return the string by just using getValue()? I don’t know how to apply the variable into the parent function. I’m getting headache:
<script>
function getValue() {
new Request.JSON({
data: JSON.encode({
"serviceName": "demoTest",
"methodName": "someValue",
"parameters": []
}),
onSuccess: function(data) { // data = a string with some text
var string = data;
console.log(string); // returns the string as expected
},
url: "gateway/?contentType=application/json"
}).send();
return string; // returns undefined
}
</script>
The problem here is that the
returnis fired long before theonSuccess(callback) function is triggered. One approach would be to use also a callback function. The way you want to do it won’t work.Here is your getValue function:
Here is a callback function:
And finally call the function on document loaded: