2017-07-27 6 views
1

Ich versuche, ein JSON von einer Anwendung kommen und in der Datenbank gespeichert, versuche ich dies mit PHP und MySQL.Get json und speichern mit PHP

Ich brauche einen JSON und dann speichern Sie es auf der Bank. Ich kann JSON nicht bekommen und es auf eine Variable weitergeben. Speichern Sie die Variable in der Datenbank, die ich tun könnte.

Ich habe eine einfache Tabelle in der Bank erstellt und diese ich erstellt ist einfach, es ist nur zu lernen.

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(); 
?> 

Ich erhalte json von AngularJS, dass AngularJS Funktion json Senden:

.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); 
    } 
}) 

Aber ich kann nicht die json bekommen, die von AngularJS in PHP kommt, Könnte mir jemand helfen?

Antwort

0

Ich habe wieder geändert.

Receives_json.php

<?php 

header("Access-Control-Allow-Origin: *"); 

$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(assunto_contato, comentario_contato, nome_contato, data_contato)'; 
$sql_insert .= 'VALUES (?, ?, ?, ?)'; 

$stm = $conn -> prepare($sql_insert); 
$stm->bind_param("ssss", $assunto_contato, $comentario_contato, $nome_contato, $data); 

if ($stm->execute()){ 
    $retorno = array("retorno" => 'YES'); 
} else { 
    $retorno = array("retorno" => 'NO'); 
} 

echo json_encode($retorno); 

$stm->close(); 
$conn->close(); 
?> 

mein AngularJS

.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({ 
      method:'POST', 
      url:linkContato, 
      data:{ 
       "assunto_contato" : $scope.contato.assunto, 
       "comentario_contato" : $scope.contato.comentario 
       }, 
      headers: { 'Content-Type': 'application/json; charset=utf-8' } 
      }).then(function(resp){ 
      console.log(resp); //success 
      }, function(error){ 
       //if error 
      }); 

      $ionicPopup.alert({ 
       title: 'Sua mensagem foi enviada' 
      }).then(function(){ 
       $state.go('app.home'); 
      }) 


      console.log($scope.contato.assunto); 
      console.log($scope.contato.comentario); 
    } 

}) 

In der Datenbank erzeugt einen Datensatz, aber ich kann nicht die Variablen aus AngularJS kommt bekommen. Und es gibt keine Fehler auf der Chrome-Konsole.

Weiß jemand, was ich in PHP falsch mache?

+0

Weiß jemand, was ich falsch mache in PHP? @ jesus-carrasco –

+0

Weiß jemand, was ich falsch in PHP mache? @vivz –

+0

Ihr js arbeitet jetzt? Welchen Fehler bekommst du jetzt? @ Lucas – Vivz

0

Sie müssen die Post-Methode korrekt mit dem gewünschten Objekt aufrufen. So Ihr Code ändern, um die unten Art und Weise und Zugriff auf die Antwort von Ihrem API-Aufruf

var data = { 
    "assunto_contato": $scope.contato.assunto, 
    "comentario_contato": $scope.contato.comentario 
} 
data = JSON.stringify(data); 
var linkContato = 'http://apps.greenonetec.com.br/insert_contato.php'; 
$http.post(linkContato, data).then(function(response) { 
    console.log(response.data); 
}).catch(function(error) { 

}); 

Für weitere Informationen folgende Weise kommen: https://docs.angularjs.org/api/ng/service/ $ http

+0

Diese Verwendung ist nicht in der Dokumentation für das $ http-Objekt enthalten. Kannst du bitte eine Quelle posten? – catbadger

+0

Es stammt aus der $ http-Dokumentation '$ http.post ('/someUrl ', data, config).dann (successCallback, errorCallback); ' – Vivz

0

Es scheint eine gewisse Verwirrung. Laut der Dokumentation unter https://docs.angularjs.org/api/ng/service/ $ http verwenden Sie nicht $ http rechts. haben einen Blick auf die Dokumente dort, und Sie sollten mit etwas wie am Ende:

$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. 
    }); 
0

Ich änderte den AngularJS, zu testen. Ich tat wie @Vivz sagte. Ich denke, dass er so ist:

.controller('contatoController', function($scope, $stateParams, $ionicPopup, $ionicHistory, $http, $ionicPlatform, $state) { 

     $scope.contato = {}; 

     $scope.Enviar = function(){ 

        var data = { 
         "assunto_contato": $scope.contato.assunto, 
         "comentario_contato": $scope.contato.comentario 
        } 
        data = JSON.stringify(data); 
        var linkContato = 'http://apps.greenonetec.com.br/insert_contato.php'; 
        $http.post(linkContato, data).then(function(response) { 
         console.log(response.data); 
        }).catch(function(error) { 

        }); 

       $ionicHistory.nextViewOptions({ 
         disableBack : true 
       }) 

       $ionicPopup.alert({ 
        title: 'Sua mensagem foi enviada' 
       }).then(function(){ 
        $state.go('app.home'); 
       }) 
     } 

}) 

Aber jetzt gibt er diesen Fehler:

OPTIONS http://apps.greenonetec.com.br/insert_contato.php 500 (Internal Server Error) - ionic.bundle.js: 25005

XMLHttpRequest nicht http://apps.greenonetec.com.br/insert_contato.php laden. Antwort für Preflight hat ungültigen HTTP-Status-Code 500

+0

Der erste Fehler wird möglicherweise verursacht, weil Ihr PHP nicht erwartet, was Sie senden. Leider kenne ich PHP nicht gut, um zu überprüfen, ob dein PHP-Code funktioniert oder nicht. Aber Ihr 2. Fehler kann behoben werden, indem Sie diese https://stackoverflow.com/questions/33660712/angularjs-post-fails-response-for-preplight-has-invalid-http-status-code-404 überprüfen. Versuche den zweiten zu lösen, vielleicht wird der erste auch auflösen. – Vivz

0

ändern Sie Ihre $ http wie diese .. Ich erkenne, dass Sie die $ Scope-Werte waren. da sind Unrecht.

.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({ 
      method:'POST', 
      url:linkContrato, 
      data:{ 
       "assunto_contato" : $scope.contato.assunto, 
       "comentario_contato" : $scope.contato.comentario 
       }, 
      headers: { 'Content-Type': 'application/json; charset=utf-8' } 
      }).then(function(resp){ 
      console.log(resp); //success 
      }, function(error){ 
       //if error 
      }); 


}) 
+0

über die Prefligh-Check erlauben Header, erlauben Methoden erlauben Herkunft in Ihrer Konfiguration, einige .htacces Regeln oder in Ihrer index.php –