我正在尝试从应用程序获取一个 json 并保存在数据库中,我正在尝试使用 php 和 mysql 执行此操作。
我需要得到一个 json,然后将其保存在银行中。我无法获取 json 并将其传递给某个变量。将变量保存在我能做的数据库中。
我在银行创建了一个简单的表,我创建的这个很简单,只是为了学习。
Receives_json.php
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 1000");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE");
$assunto_contato = $_POST["assunto_contato"];
$comentario_contato = $_POST["comentario_contato"];
$nome_contato = $_POST["nome_contato"];
$data = date("Y-m-d H:i:s");
$conn = new mysqli("", "", "", "");
$sql_insert = "INSERT INTO contato VALUES ('$assunto_contato', '$comentario_contato', '$nome_contato', '$data')";
$stm = $conn -> prepare($sql_insert);
if ($stm->execute()){
$retorno = array("retorno" => 'YES');
} else {
$retorno = array("retorno" => 'NO');
}
echo json_encode($retorno);
$stm->close();
$conn->close();
?>
我从 angularjs 获取 json,angularjs 函数发送 json:
.controller('contatoController', function($scope, $stateParams, $ionicPopup, $ionicHistory, $http, $ionicPlatform, $state) {
$scope.contato = {};
$scope.Enviar = function() {
var linkContato = 'http://apps.greenonetec.com.br/insert_contato.php'
$http.post(linkContato, {
"assunto_contato": "$scope.contato.assunto",
"comentario_contato": "$scope.contato.comentario"
});
console.log()
$ionicHistory.nextViewOptions({
disableBack: true
})
$ionicPopup.alert({
title: 'Sua mensagem foi enviada'
}).then(function() {
$state.go('app.home');
})
console.log($scope.contato.assunto);
console.log($scope.contato.comentario);
}
})
但是我无法在 php 中获取来自 angularjs 的 json,有人可以帮助我吗?
最佳答案
似乎有些困惑。根据 https://docs.angularjs.org/api/ng/service/ 处的文档$http 你没有正确使用 $http。看看那里的文档,你应该得到类似的东西:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
关于php - 获取json并用php保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45356585/