I have being trying to search this here on Stackoverflow, but because I do not know what it is called I am having trouble.
How do I do this?:
My Javascript code looks like this:
return fetch('http://www.mywebsite.com/backend.php', {
method: "post",
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
myFloat: 3.14,
myString: "Hello"
})
})
.then((response) => response.json())
.then((data) => {
// How do I get these?
let returnFloat = ???;
let returnString = ???;
});
So what does my php file look like?
<?php
$myFloat = ?????;
$myString = ?????;
$returnFloat = 2.0 * $myFloat;
$returnString = $myString . " and Goodbye"; // "Hello and Goodbye"
// Now how do I pack this up to send it back???
// I want to send back $returnFloat and $returnString
?>
So my questions are these:
How do I set
$myFloat = ???;
and$myString = ???;
making sure these are a number and a string respectively (and not both strings)?In the php I have the line
// Now how do I pack this up to send it back???
. I want to send back$returnFloat
and$returnString
. How do I pack them up to send back?Once these two bits of php data in (2) are sent back, how do I unpack it back in my javascript with the lines
let returnFloat = ???;
andlet returnString = ???;
making sure both are a float and a string respectively?Are there any issues of cross-website security I need to know about? Ie htaccess or the windows server equivalent. If there are, what do they look like?