I don’t have a photon at home but thought I would test the new javascript async inline function used on a webpage to do asynchronous AJAX style calls. The “await” ability removes the callback mess of the normal AJAX call.
Can someone try this code and see if it works with an active photon. Just paste all the code into a webpage file saved with the .html extension.
replace your photon ID and Access token where needed:
Works for me (connected = true).
But to test it you won’t need a Photon at home, after all you are just asking the cloud about what it knows of the device - connected or not
I see, I thought you just wanted that exact script tested.
I can try with a Particle.variable() as this uses a GET request just as the device info request your tried - and that works too.
Thanks @ScruffR. Nothing wrong with AJAX commands with callbacks, they are just a bit difficult to understand and teach to others.The above commands are a bit easier to understand the flow of information and for beginners to write.
I assume all the cloud API commands at https://docs.particle.io/reference/api/ can be done this way without having to wait for a web page refresh. Just remember not to hard code your device ID and Access Token into the webpage.
Perhaps someone can post some working code that gets a variable, etc.
but I can’t seem to get a fetch command that posts the equivalent of form data, (which should not be very hard.) here is my code: (using your own access token and device ID)
I can do everything I want using a very old form POST method that sends the formdata to a different target and that works fine, but it is very cumbersome. Anyone any ideas for getting the above code working. Not sure if it is missing headers or what.
Here is my working code. It is kind of pre-AJAX sending the form data to another textarea instead of refreshing the page. I am trying to update the programming style. On this one you have to enter the device ID and access token on the webpage. It works with the above .ino.
<script>
function myConnect(){
document.myForm.action = 'https://api.particle.io/v1/devices/' + document.all.myDeviceId.value + '/'+ document.all.myFunctionName.value
document.all.mySend.value = document.myForm.action
document.myForm.submit();
}
</script>
Device ID: <input id="myDeviceId" name="myCoreID" type=password size=50 placeholder="777777777777777777777"><br>
Function to run: <input id="myFunctionName" name="myFunction" type=text value="doAll" >
<form name="myForm" method="POST" target="myFrameName"><br>
Access Token:<input id="myToken" name="access_token" type=password size=50 placeholder="fc7777777777777777777777777777777777"><br>
Sent Parameter:<input id="myParameter" name="args" type=text value="toggleLED"><br>
</form><br>
<input type="button" style="font-size:25px" value="Toggle LED" onClick="{
myConnect();
}"><br><br>
Results show here (may set them hidden if you want!)<br>
<input type=text id="mySend" size=100 value="">
<iframe style="display:inline" width="400" height="200" name="myFrameName" id="myFrameId"></iframe><br>