03-17-2020 08:09
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-17-2020 08:09
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi! I am trying to use class XMLHttpRequest to GET and POST some data from my server. I use the function below:
{
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?

03-17-2020 10:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-17-2020 10:07
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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/

03-17-2020 20:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-17-2020 20:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Hi qichuan!
I tried to use fetch before. The code is like this
.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!

03-17-2020 20:30
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-17-2020 20:30
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
fetch() will only work using https (and not self-signed), unless you host on 127.0.0.1
Gondwana Software

03-17-2020 21:14
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

03-17-2020 21:14
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
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!

03-17-2020 21:53
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


03-17-2020 21:53
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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.
Gondwana Software
05-19-2020 19:37
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

05-19-2020 19:37
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
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!
