Javascript fetch
-
I am trying to fetch the windy data, but get an error;
Unexpected token W in JSON at position 0
at /node_modules/node-fetch/lib/body.js:48:31
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
name: 'FetchError',
message: 'invalid json response body at https://api.windy.com/api/point-forecast/v2 reason: Unexpected token W in JSON at position 0',
type: 'invalid-json'What could be the problem in my script?
let _data = { "lat": 52.046, "lon": 4.447, "model": "iconeu", "parameters": ["wind"], "levels": ["surface"], "key": "..."};
fetch("https://api.windy.com/api/point-forecast/v2", {
method: 'POST',
body: _data,
headers: {"Content-type": "application/json; charset=UTF-8"}
})
.then(response => response.json())
.then(Server => {
console.log(Server) // Prints result fromresponse.json()
// var UvNow = Server.result.uv;//Processing UVToday
// BLApp.apiPut("UvNow/"+UvNow);//Updating better logic variable UVToday
})
.catch(error => console.error(error))
// OpenUvServer = false
// var StandardProcessing = false
// console.log("i is: " +i) -
You should look at the windy repository with plugin examples.
Check this plugin.
-
Also, you should not publish your key for Point Forecast API. I removed it from your post. Unlike from eg. Map Forecast, Point Forecast is a server-2-server servise and it is not possible to secure your key against missusing.
-
thx, but i changed the key efore posting avoiding abuse of my key. at this moment i still can't get the fetch done. the plugin is webbased? I try to get it done with javascript on a computer
-
@Marcelklim said in Javascript fetch:
fetch("https://api.windy.com/api/point-forecast/v2", {
method: 'POST',
body: _data,
headers: {"Content-type": "application/json; charset=UTF-8"}
})You should try
body: JSON.stringify(_data)