如何使用 nativescript 中的 fetch 将表单数据发送到 php?
How do i send form data to php using fetch in nativescript?
我正在尝试在我的 nativescript 应用程序中创建注册表单,我正在使用 fetch api 到 POST 数据。但现在它只是填充数据库而没有实际上传数据。这是我试过的
JS
const phpData = {
email: "user@nativescript.org",
password: "password",
name: "Adekunle Adeyeye",
number: "07019888741",
}
/*email = viewModel.getViewById("email").value;
password = viewModel.getViewById("password").value;
name = viewModel.getViewById("name").value;
number = viewModel.getViewById("number").value;*/
fetch("https://goodsbuy.000webhostapp.com/register.php", {
method: 'POST',
body: JSON.stringify(phpData),
headers:{
"content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => console.log(json))
.then((phpData) => {
console.log('Success:', phpData);
this.set("processing", false);
Toast.makeText("Successful").show();
this.isLoggingIn = true;
})
.catch((error) => {
console.error('Error:', error);
this.set("processing", false);
Toast.makeText("Account Already exists").show();
this.isLoggingIn = false;
});
PHP
$email = $_POST['email'];
$password = $_POST['password'];
$name = $_POST['name'];
$number = $_POST['number'];
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO comedyapp(`name`, `email`, `number`, `password`) VALUES ('$name', '$email', '$number', '$password')";
if ($conn->query($sql) === TRUE) {
echo '{"items":'. json_encode('Good') .'}';
} else {
echo '{"items":'. json_encode('Bad') .'}';
}
请帮助我。
替换
$email = $_POST['email'];
$password = $_POST['password'];
$name = $_POST['name'];
$number = $_POST['number'];
和
extract(json_decode(file_get_contents('php://input'),true));
它会自动声明变量。
我正在尝试在我的 nativescript 应用程序中创建注册表单,我正在使用 fetch api 到 POST 数据。但现在它只是填充数据库而没有实际上传数据。这是我试过的
JS
const phpData = {
email: "user@nativescript.org",
password: "password",
name: "Adekunle Adeyeye",
number: "07019888741",
}
/*email = viewModel.getViewById("email").value;
password = viewModel.getViewById("password").value;
name = viewModel.getViewById("name").value;
number = viewModel.getViewById("number").value;*/
fetch("https://goodsbuy.000webhostapp.com/register.php", {
method: 'POST',
body: JSON.stringify(phpData),
headers:{
"content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => console.log(json))
.then((phpData) => {
console.log('Success:', phpData);
this.set("processing", false);
Toast.makeText("Successful").show();
this.isLoggingIn = true;
})
.catch((error) => {
console.error('Error:', error);
this.set("processing", false);
Toast.makeText("Account Already exists").show();
this.isLoggingIn = false;
});
PHP
$email = $_POST['email'];
$password = $_POST['password'];
$name = $_POST['name'];
$number = $_POST['number'];
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO comedyapp(`name`, `email`, `number`, `password`) VALUES ('$name', '$email', '$number', '$password')";
if ($conn->query($sql) === TRUE) {
echo '{"items":'. json_encode('Good') .'}';
} else {
echo '{"items":'. json_encode('Bad') .'}';
}
请帮助我。
替换
$email = $_POST['email'];
$password = $_POST['password'];
$name = $_POST['name'];
$number = $_POST['number'];
和
extract(json_decode(file_get_contents('php://input'),true));
它会自动声明变量。