Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using GET and POST by XMLHttpRequest

Hi! I am trying to use class XMLHttpRequest to GET and POST some data from my server. I use the function below:

Spoiler
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( 'GET', theUrl, false ); // false for synchronous request
xmlHttp.send();
return xmlHttp.responseText;
//console.log(xmlHttp.responseText);
}

 When I try this code on a static HTML file, it works and I can get data from my server. But when I use it on the companion, I got error "NetworkError: Failed to execute 'send' on 'XMLHttpRequest':". I found that if I set xmlHttp.open( 'GET', theUrl, false );  to xmlHttp.open( 'GET', theUrl, true ); the program on the companion works but I can not get any data from my server since the  resdyState of xmlHttp is always 1. What should I do? Or is there any successful way to GET or POST data from a server from companion?  

Best Answer
0 Votes
6 REPLIES 6

Why not using fetch() API? it is modern, well-documented and much easier to use than XMLHttpRequest.

 

See https://dev.fitbit.com/build/reference/companion-api/fetch/

Best Answer
0 Votes

Hi qichuan!

I tried to use fetch before. The code is like this 

Spoiler
fetch('http://192.168.0.51:8000/')
.then(function(response) {
return response.text();
})
.then(function(myJson) {
console.log(myJson);
});

but I still get 'Uncaught (in promise) TypeError: Failed to fetch'. But this code works on a static html file. The IP is in the same router.... Why did this happen? Thank you!

Best Answer
0 Votes

fetch() will only work using https (and not self-signed), unless you host on 127.0.0.1

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Hi Gondwana!

I will manage to try to https...

Actually I just want to transfer sensors' realtime data to my local server within the same router. The companion can not communicate with my local server directly? Or you mean host on 127.0.0.1 on the companion? 

Thank you!

Best Answer
0 Votes

I've managed to host a server on the companion device (127.0.0.1) to collect real-time sensor data. It wasn't very elegant, though.

I think that hosting your server on any other device, including those on your LAN (same router), will require https. Self-signed doesn't seem to be a goer. A possible way around this is to get your browser to store a security exception for the connection, which may require browsing to the server prior to running your Fitbit app. I gather that different browsers handle this very differently, so it may not work.

Peter McLennan
Gondwana Software
Best Answer

After spending several days on this problem. THAT'S what they mean?!


"Please note: fetch() can only be used to access https endpoints and resources,except when accessing resources on a local network by IP address."

Actually got my app up and running in an evening, but when deployed to my physical devices it's been failing with the "uncaught (in promise)" errors, even with a .catch added.

 

I thought I must just be misconfiguring the fetch, trying everything. This documentation is so frustrating, and the emulators a patchwork implementation at best. >.<

 

Not the first mis-documented method I've had to work around.. first was misleading websocket information, then works-on-emulator-not-devices fetches that didn't accept spaces in the URI.

 

Genuine thank you for the help!

Best Answer