Problems when trying to send A request to my web server, from an app created with Ionic Framework

2

I have a problem with app híbrida , I have two input fields where I put username and password and that sent it to a URL in which is the script PHP hosted (web server that is free) and that request is blocked.

From what I have read it has to do with browser security ( Cors ) What can be done in this case? I've tried it locally, building apk for android and nothing, I've added a complement of proxyalgo and nothing, some help please.

This is part of the code with which I try to make my requests

.controller('loginCtrl', function($scope,$rootScope,$http) {

$scope.logear = function($scope){



$scope.mensaje = $rootScope.mensaje;
//alert($rootScope.mensaje);
var url = "http://185.27.134.110/carpeta/php/login.php";

var emaillog = $scope.emailLog;
var passlog = $scope.passlog;

$http.post(url,{'emaillog':emaillog,'passlog':passlog,'accion':'logear'})
        .success(function(data,status,headers,cofing){
            console.log(data);
            console.log(status);
            console.log(headers);
            console.log(cofing);

            // SI EL ESTADO ES TRUE SE REDIRECIONA AL LA PANTALLA PRICIPAL DE INICIO SESION
            if(data.estado== true){

                        //SE REDIRECIONA AL ACCESO PARA PODER EFEFTUAR LLAMADAS
                        window.location="http://185.27.134.110/carpeta/#/acceso";

                        //ASIGNAMOS VALORES A VARIABLES GLOBALES PARA UTILIZARLA EN LA SIGUEINTE VISTA
                        $rootScope.mensajeinicio = data.contenido;
                        $rootScope.nombreUser = data.contenido;

            }else{

                    window.location="http://185.27.134.110/carpeta/#/login";
            }

            //alert(data.existe);


        });
}
})

and code PHP

<?php

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

include_once 'class.Conexion.php';

$data = json_decode(file_get_contents("php://input"));

$email = ($data->email);
$accion = ($data->accion);


$salidaJson = "";
$contenido = array();
//$contenidoDos = array();
$estado = false;
$existe = "false";

if($accion == 'validarDatos'){

    $pass = ($data->pass);
    $nombre = ($data->nombre);


    $db = new conexion(); 

    $sql = $db->query("SELECT * FROM registro WHERE email ='".$email."' ");

        if($db->rows($sql)> 0){
            //SI EXISTE EL EMAIL SE DEBE CARGAR ALA PANTALLA DE INICIO DE SESION
            //DE LO CONTRARIO SE REGISTRARA
            while($rew = $db->recorrer($sql)){

                $contenido = array(
                    'nombre' => $rew['nombre_completo'],
                    'email' => $rew['email'],
                    'respuesta' => 'Email Registrado - Inicie Sesion'
                );

            }
            $estado  = true;
            $existe = true;
        }else{

            $sql = $db->query("INSERT INTO registro(nombre_completo,email,pass)VALUES('".$nombre."','".$email."','".$pass."')");
                if(!mysql_error()){

                    $contenido = array("respuesta" => 'REGISTRO REALIZADO CON EXITO');
                    $estado = true;
                    $existe = true;
                }else{

                    $contenido = array(
                    "respuesta" => 'ERROR AL INTENTENTAR REGISTRARSE, PORFAVOR INTENTELO NUEVAMENTE!!');
                    $estado = false;
                    $existe = false;


                }

            //$estado = false;
        }
}elseif($accion == 'getDatos'){


    $db = new conexion(); 

    $sql = $db->query("SELECT * FROM registro WHERE email ='".$email."' ");

    if($db->rows($sql)> 0){
        //SI EXISTE EL EMAIL SE DEBE CARGAR ALA PANTALLA DE INICIO DE SESION
        //DE LO CONTRARIO SE REGISTRARA
        while($rew = $db->recorrer($sql)){

            $contenido = array(
                'nombre' => $rew['nombre_completo'],
                'email' => $rew['email']
            );

        }
        $estado  = true;
    }
}elseif($accion == 'getLineas'){

    $db = new conexion(); 

    $sql = $db->query("SELECT * FROM lineas");

    if($db->rows($sql)> 0){
        //SI EXISTE EL EMAIL SE DEBE CARGAR ALA PANTALLA DE INICIO DE SESION
        //DE LO CONTRARIO SE REGISTRARA
        while($rew = $db->recorrer($sql)){

            $contenido = array(
                'nombreLinea' => $rew['nombre_linea'],
                'numeroLinea' => $rew['numero_linea']
            );

        }
        $estado  = true;
    }else{
        $estado = false;
    }

}




$salidaJson = array("contenido" => $contenido,
                   "estado" => $estado,
                   "existe" => $existe);

//header('Context-type: application/json');
echo json_encode($salidaJson); 

// mysqli_close($sql);   
?>

Error:

  

CORS Crossover blocked request: The same origin policy does not allow the reading of remote resources in 185.27.134.110/folder/php/consultas.php. (Reason: CORS header 'Access-Control-Allow-Origin' not present)

    
asked by Dagg 31.08.2016 в 03:23
source

2 answers

1

In your Script PHP add these headers:

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
?>

In case it does not work for you, in the file .htaccess of the folder where your script is located:

Header set Access-Control-Allow-Origin "*"
    
answered by 31.08.2016 в 03:43
1

Sorry, you do not put your error, then it is difficult to know if it is from the client or server side, so I leave this here, it could be that the way you are sending the data is not correct, I apply this, when you ask a question:

The data variable is an object that I capture from a form to send to the server. the function that I put there, is in a factory

 function Autenticacion(datos) {

    var url = 'ruta/del/servidor';

    return $http.post(url, $httpParamSerializer(datos), {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });

};

Here's the question where in one they explain to me that I was doing wrong when I sent the object to the server data transmission and injection

    
answered by 20.09.2016 в 23:52