So here is my code I can’t get to work. I have read the docs a few times and am still not grasping this. I know the programming on the photon is good, as it works with some javascript and ajax on web page. I just need to use the javascript fetch call for another project.
Any suggestions?
var appHeading = "Led Control";
var core1ID = "53ff6a06666757482439xxxx";
var accessToken = "8b83c6bddbf8c73f0e835362cff88exxxxxxxxx";
var baseURL = "https://api.spark.io/v1/devices/";
var options = {
"method": "post",
"headers": {
"Authorization": "bearer {8b83c6bddbf8c73f0e835362cff88e90xxxxxxxx}"
}
};
fucntion on() {
fetch('https://api.spark.io/v1/devices/53ff6a0666675748243xxxx/on', options)
};
That URL is structured as a GET request, which should actually be a POST request. They need to be treated differently. This guide has been very helpful for this:
@Moors7 First off, thank you for taking the time to respond.
SO here is where I am confused. I am trying to make a POST request via the fetch command. I can’t use AJAX where I am trying to implement this. Am i missing something from my code? Do i need to provide a body when using fetch?
And that did not work. So I created another function to test:
// This DOES work:
const turnOnOff2 = (device,toggle) => {
post.headers['Content-Type']='application/x-www-form-urlencoded'
post.body = 'arg='+toggle
return fetch(api+device+cmd, post).then(j=>j.json()).then(o=>o)
}
So I was able to get my code going, but still bothering me why the ‘application/json’ would not work. After a couple hours playing around I tried this to my success: