I am using JSONP to collect data from the user but do not require the user to get a response.
Therefore I want to send the user an instant response so they can continue without having to wait for the server processing.
How do I send them a response but continue processing?
I'm using Google Script but I guess any javascript way to return a response and continue processing should work too.
I have something like:
function handleResponse(e) {
//do something with e
return ContentService
.createTextOutput('console.log("updated")')
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
I would like to return the response and then "do something with e".
Edit: Ok after a lot of mucking around I have a semi-working solution (There are always roadblocks!)
Currently I have:
var SCRIPT_PROP = PropertiesService.getScriptProperties();
function doGet(e){
SCRIPT_PROP.setProperty("myParameters", e.parameters);
ScriptApp.newTrigger("handleResponse")
.timeBased()
.after(20 * 1000)
.create();
return ContentService
.createTextOutput('console.log("processing")')
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
function handleResponse() {
Logger.log(SCRIPT_PROP.getProperty("myParameters"));
}
What it's doing is saving the data from the user to a global like variable. Then it sets up a trigger to execute the handleResponse() function after 20 seconds. And finally it returns something to the user so they do not have to wait for the handleResponse() function to finish.
Now for the problems I am having with this solution, it seems to be hit and miss, it will sometimes fire the handleResponse() function and sometimes never do it.
From the docs it says that triggers will execute at the time you specify +/- 15 minutes! Now in the times that it works I have seen it take from 10 seconds to 45 seconds. In the times that it has not worked I have waited 20 minutes and still nothing. It seems the shorter I set the trigger, the more times it never executes.
The other problem I have is that I can only have 14 triggers at once so if they decide to take 15 minutes to execute I can easily hit that limit.
Is there any other way to get a solution like this to work?
JSONP stands for JSON with Padding. Requesting a file from another domain can cause problems, due to cross-domain policy. Requesting an external script from another domain does not have this problem. JSONP uses this advantage, and request files using the script tag instead of the XMLHttpRequest object.
JSONP is an informal protocol that allows you to make cross-domain calls by producing script tags on the current page and awaiting a response to a callback handler you provide.
We can only use JSONP when: The API itself supports JSONP . It needs to return the JSON response wrapped in a function and it usually lets us pass in the function name we want it to use as one of the query params. We can only use it for GET requests, it doesn't work for PUT / POST / DELETE and so on.
if the only requirement is to stop the window loading action on the frontend, as clarified in the comments, then consider wrapping the script tag insertion inside setTimeout()
...
function createScriptTag() {
setTimeout( function() { // throw task in JS queue to prevent window load
var scriptTag = document.createElement('script');
scriptTag.src = "SCRIPT_URL?prefix=myCallback"; // include parameters if needed
document.body.appendChild(scriptTag);
}, 0);
}
createScriptTag();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With