使用离子的post方法将数据发送到服务器

编程入门 行业动态 更新时间:2024-10-12 03:17:44
本文介绍了使用离子的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 = '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方法将数据发送到服务器

本文发布于:2023-10-10 22:24:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   离子   服务器   方法   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!