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

POSTing problem or Server problem?

ANSWERED

Hi everyone.

I'm beginner for Fitbit studio, Javascript, and PHP, so I can't find where the problem is.

 

I made device application in which the array of HeartRate in 20 seconds and the timestamp were sent to companion smartphone when I tapped the button, using messaging API. 

This worked well.

The sent data  was like this (when JSON.stringfied).↓

data={"heartRateArray:[84,84,84,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],

"nowdate":"Mon Mar 15 2021 14:19:12 GMT+09:00"}

 

To save the data in the companion device, I built a local server running in Android, installing KSWeb. 

I posted the data using fetch() to getJSON.php.

It seemed to work well ,because I could get "Success"

import * as messaging from "messaging";

messaging.peerSocket.addEventListener("message", (evt) => {
  console.error(JSON.stringify(evt.data));
  sendServer(evt.data);
  
});

//Send to server
async function sendServer(sentdata){
  await fetch('http://127.0.0.1:8080/getJSON.php', {
      method: 'POST',
      headers:{'Content-Type': 'application/json'},
      body: JSON.stringify(sentdata)
    })
    .then(response => {console.log("Success");})
    .catch(error => {console.log("Failed!!You'll fail to get PhD.!!!!!");})    
}

 

Since I want to confirm that the local server could  get the data sent from companion app, I made getJSON.php file below.

The localhost page showed "Get JSON!", NULL.

 

<html>
	<head>
		<title>Test</title>
      <meta http-equiv = "Content-Type" content = "text/html; charset=utf-8">
	 </head>
			<body>
				<h2>Get JSON!</h2>					
				<?php
				// get POST data
				$json = file_get_contents("php://input");

   				// JSONstring to array				
					$contents = json_decode($json,true);
				// Debug
					var_dump($contents);
				?>
			</body>
</html>

I have no idea why the localhost page displays NULL.

The code in the getJSON.php  is wrong?

Or the data have not been posted to localserver?

 

Please help.  

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

body:data in my case data is a JSON string. This code 100% works.  This api call is used for more than 100 times a day. my url is an https, but that is only necessary for remote servers.

 

Then it might be your local server not responding well. This is my php code

 

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$round = json_decode(file_get_contents('php://input'), true);

 

 

 

View best answer in original post

Best Answer
0 Votes
4 REPLIES 4
  fetch(url,{method: "POST", body: data}).then(res => res.json()).then(response => { 
    console.log("response "+JSON.stringify(response));   
  }).catch(e => {
    console.log("Upload failed"+JSON.stringify(e)+" "+url);
  });  

Hope my code snipped from below helps. I think you have to parse your data to json instead of setting the header Content-Type to Json

 

 

Best Answer
0 Votes

Thank you for your advice.

I tried your code, but  I got 'Upload failed'.....😭

Best Answer
0 Votes

body:data in my case data is a JSON string. This code 100% works.  This api call is used for more than 100 times a day. my url is an https, but that is only necessary for remote servers.

 

Then it might be your local server not responding well. This is my php code

 

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$round = json_decode(file_get_contents('php://input'), true);

 

 

 

Best Answer
0 Votes

Thank you for the further information.

I understood the problem was in my php code.

It will takes a time to solve the problem in my php code ,because I have little knowledge about php.

(This is my first experience to write php code.)

I'll start to learn the basics of php code.

Best Answer
0 Votes