使用离子的post方法将数据发送到服务器 [英] Sending data to a server using post method in ionic

查看:98
本文介绍了使用离子的post方法将数据发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用离子开发的post方法将数据发送到服务器,问题是我不明白poque不起作用。

I am trying to send data to a server with a post method from an ionic development , the problem is I do not understand poque not work.

<ion-view view-title="Guardar estudiante">
  <ion-content class="padding">
<form>
  <div class="list">

    <label class="item item-imput">
      <input type="number" placeholder="Documento" ng-model="Usuario.idu">
    </label>


    <label class="item item-imput">
      <input type="text" placeholder="Nombre" ng-model="Usuario.nom">
    </label>
    <label class="item item-imput">
      <input type="text" placeholder="Apellidos" ng-model="Usuario.ape">
    </label>

    <label class="item item-imput">
    <input type="text" placeholder="Direccion" ng-model="Usuario.dir"> 
    </label>

    <label class="item item-imput">
           <input type="number" placeholder="Telefono" ng-model="Usuario.tel">  
    </label>

    <label class="item item-imput">
    <input type="text" placeholder="Email" ng-model="Usuario.cor"> 
    </label>

    <label class="item item-imput">
           <input type="text" placeholder="Contraseña" ng-model="Usuario.pas">
    </label>



    <label class="item item-input">
      <span style="text-align: center; width: 100%;">
         <button 
         class="button icon-left" ng-click="guardarEstudiante()">
         GUARDAR 
         </button> 
      </span>
    </label>
  </div>
</form>

<p ng-repeat='u in users'>{{u.Nombre}} </p>
<p>{{ PostDataResponse }}</p>


angular.module('starter.controllers', [])    
.controller('GuardarEstudianteCtrl',    
   function($scope, $http){
  $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-    urlencoded;charset=utf-8';
var defaultHTTPHeaders = {
  'Content-type': 'application/json',
  'Accept' : 'application/json'
};
$http.defaults.headers.post = defaultHTTPHeaders;
  $scope.Usuario =
{
     idu : '',
     nom : '',
     ape : '',
     dir : '',
     tel : '',
     cor : '',
     pas : '' 
};
  $scope.guardarEstudiante = function(){
       // alert($scope.Usuario.Apellidos)
   var urlCompleta = 'http://www.******.co/*******.php?';
   //var postUrl     = $sce.trustAsResourceUrl(urlCompleta);

   UsuarioObj  = $scope.Usuario;

   $http.post(urlCompleta, $scope.Usuario)

   .then(

        function(data, status, headers, config){
          if (data.data.success  != 1){

           alert('Error En Registro, '+ data.data.message+" Success-->     "+data.data.success+" \n user ="+ $scope.Usuario.nom+" \napellido "+$scope.Usuario.ape);
      }
     else
     {
      alert('Usuario Registrado, '+ data.data.message+" Success-->     "+data.data.success+" \n user ="+ $scope.Usuario.nom );
     }

         console.log('Error with the API list_mobile_project');
         console.log(data);
         console.log(status);
         console.log(headers);
         console.log(config);  

           $scope.users = UsuarioObj;
           console.log("-------------User------------")
           console.log($scope.users);

        },
        function(data, status, headers, config){
          alert('Error De Codigo o de plataforma de salida de datos');

          $scope.users = UsuarioObj;
           console.log("-------------User------------")
           console.log($scope.users);

        }
    ); 
  };
    });



PHP



PHP

<?php

if (isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
    }

    // Access-Control headers are received during OPTIONS requests
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        
            {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
    }




/*
 * Following code will update a product information
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();

// check for required fields
// check for required fields

if (isset($_POST['idu'])
&& isset($_POST['nom']) 
&& isset($_POST['ape'])
&& isset($_POST['dir'])
&& isset($_POST['tel'])
&& isset($_POST['cor'])
&& isset($_POST['pas'])
) 
{

$idus = $_POST['idu'];
$nomb = $_POST['nom'];
$apel=$_POST['ape'];
$dire=$_POST['dir'];
$tele=$_POST['tel'];
$corr=$_POST['cor'];
$pass=$_POST['pas'];

  // include db connect class
   $con = mysqli_connect("localhost","******","*******","******");

// mysql inserting a new row
$result = mysqli_query($con,"INSERT INTO ******(Id***, N****, A***, D****, T****, E*****, C****, E****) VALUES($idus,'$nomb', '$apel','$dire', $tele, '$corr','$pass','Inactivo')");


 // check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Product successfully created.";

    // echoing JSON response
    echo json_encode($response);
    } else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}


?>


推荐答案

我正在使用此代码并使用各种方法 GET / POST / DELETE

I am using this code and works with various methods GET/POST/DELETE

$http({
       method: method,//GET-POST
       url: url,
       data: data,
       dataType: 'json',
       headers: {
           "Content-Type": "application/json"
       }
}).success(function (data) {//Success handling

}).error(function (data, status) {//Error handling

});

问候。

这篇关于使用离子的post方法将数据发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆