[SOLVED]Jquery getJSON calls returns 404 error


Hi after reading @bko 's nice tutorial on Reading spark vars ( Reading Spark Variables with Your Own HTML File ) I created a function to get variable data from particle. I keep getting Failed to load resource: the server responded with a status of 404 (Not Found) error on my console. can someone help me understand what is happening here?

I have checked the requestURL output and it looks exactly the same as URL i’m using in the “Control LEDs over the net” example. And YES, I am (globally) declaring “deviceID”, “varName”, and “accessToken” with the correct values for my particle board.

Can’t figure this out, what am I missing?

function getValue() {
    requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;
    $.getJSON(requestURL, function (json) {
        ledState = json.result;
        document.getElementById("ledPower").innerHTML = ledState;



What happens if you put the output of the request URL in your browser? If configured properly, you should be seeing the variable pop up.


That’s a great idea. The browser console is usually the way I debug this type of problem.

@superliminal should also check the version of jquery: cut and past the script URL for the jquery library near the top of the file into a browser window and see if you get a bunch of text or 404.


My guess, completely unconfirmed, is that there should not be a forward slash after the variable name and before the access token query arguments.

In other words, the string should be this instead:



Just tried it, and that doesn’t seem to matter. Valid idea nonetheless :smile:


@Moors7 The output of the request URL looks like this (except “deviceID”, “varName”, and “accessToken” replaced with my info)
and when I put that in the browser I get:

  "ok": false,
  "error": "Unknown Variable: false"

@bko Yes I just discovered the console in my browser, great find. I entered the jquery URL into my browser and got a bunch of text, so i guess its finding it.

@rickkas7 I already tried removing the slash and I get the same result:(


Are you sure your variable name is correct?
If you want to make sure your setup is correct (other than that page), you can try requesting your variable here (and seeing if it shows up in the first place).
If you can’t see it in the browser by pasting that link, it’s not going to work in your javascript either.


Or like this in the browser


@Moors7 Ah … in fact, yes I had the wrong variable name. :expressionless: I changed it and its now working. Thanks everyone.


Hence, such statements are a dangerous thing to make and we usually don’t tend to take them that serious anyway :wink:


Ok in order to redeem myself, I will at least post my code now that its all working.

This is very simple but I think useful demonstration of how to monitor and update particle values. All this app does is 1. check if an LED is currently powered on or off 2. update the switch (checkbox) and text on the webpage to the current state of the LED and 3. switch the LED on or off when the switch is pressed.

Particle code:

int led1 = D0;
int led2 = D7;
int stateLed = LOW;

void setup()

   pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);

    Particle.variable("ledState", &stateLed, INT);
   digitalWrite(led1, LOW);
   digitalWrite(led2, LOW);


void loop()

int ledToggle(String command) {

    if (command=="1") {
        stateLed = HIGH;
        return 1;
    else if (command=="0") {
        stateLed = LOW;
        return 0;
    else {
        return -1;


<!DOCTYPE html>

    <meta charset="UTF-8">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
<!--<link type="text/css" rel="stylesheet" href="css/style.css" />-->

    <span id="ledPower">Waiting for data...</span><br>

    <div class="container style3 subcontainer">
        <input type="checkbox" name="arg" id="switch1" onClick='toggleValue()'>
        <label for="switch1"></label>




var deviceID = "xxxxxxx";
var accessToken = "xxxxxxx";
var varName = "ledState";
var ledState;

$(document).ready(function () {

function getValue() {
    requestURL = "https://api.particle.io/v1/devices/" + deviceID + "/" + varName + "/?access_token=" + accessToken;
    $.getJSON(requestURL, function (json) {
        ledState = json.result;


function updateSwitch() {
    if (ledState == 0) {
        document.getElementById("switch1").checked = false;
        document.getElementById("ledPower").innerHTML = "led is now OFF";
    } else {
        document.getElementById("switch1").checked = true;
        document.getElementById("ledPower").innerHTML = "led is now ON";


function toggleValue(obj) {
    var newValue;
    if (ledState == 1) {
        newValue = 0;
        document.getElementById("ledPower").innerHTML = "led is now OFF";
    } else {
        newValue = 1;
        document.getElementById("ledPower").innerHTML = "led is now ON";


function sparkSetLed(newValue) {
    var setFunc = "led"
    var requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + setFunc + "/";
    $.post(requestURL, {
        params: newValue,
        access_token: accessToken
    ledState = newValue;