| 1234567891011121314151617181920212223242526272829 |
- /*---
- title: JSON from POST fields
- group: Basics
- summary: Read POST fields as globals and answer with sendJSONResp().
- setup: {"request":{"method":"POST","form":{"city":"Hong Kong","days":"3"}}}
- ---*/
- /*
- A WebApp calls its backend with ao_module_agirun("App/script.agi", data, ...).
- Every field of data becomes a global variable here. Change them in the
- Request window (Form fields) and run again.
- */
- if (typeof city === "undefined" || city === "") {
- // A missing field is simply an undefined global.
- sendJSONResp({ error: "city is required" });
- exit();
- }
- var n = parseInt(days, 10) || 1;
- var forecast = [];
- for (var i = 0; i < n; i++) {
- forecast.push({
- day: i + 1,
- city: city,
- high: 24 + (i * 3) % 5,
- low: 18 + i % 3
- });
- }
- sendJSONResp({ city: city, days: n, forecast: forecast });
|