[SOLUCIONADO] Ionic no funciona ajax

Iniciado por $francisco, Diciembre 19, 2016, 06:39:41 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Diciembre 19, 2016, 06:39:41 PM Ultima modificación: Enero 11, 2017, 04:52:04 PM por Stiuvert
Muy buenas, llevo unas semanas intentando hacer funcionar ajax en ionic pero no logro hacerlo, he visto ya la página oficial de ionic y angularjs y sucede algo extraño, con angularjs si funciona pero con ionic no logro retornar datos de una página con una petición ajax

app.js

Código: javascript
angular.module('starter', ['ionic'])

.config(['$sceDelegateProvider', function($sceDelegateProvider) {
  // We must whitelist the JSONP endpoint that we are using to show that we trust it
  $sceDelegateProvider.resourceUrlWhitelist([
    'self',
    'https://*/**'
  ]);
}])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      // Don't remove this line unless you know what you are doing. It stops the viewport
      // from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }

  });
})

.controller("movies",function($scope,$http, $rootScope, $ionicPlatform) {
    $ionicPlatform.ready(function() {
      var page = 'http://www.repelis.tv/buscar/?s=torrente';
      $.ajax({
        crossDomain:true,
        crossOrigin:true,
        url:'http://127.0.0.1:8080/llantas/index.php',
        dataType:'jsonp',
        data:{'name':page},
        type:'GET'
      })
      .done(function(data){alert('correcto');})
      .fail(function(jqx,s,th){alert('err');});
      });
     
});


index.php

Código: php
<?php
$user     = $_GET['name'];

$arr = array('consulta' => "$user");
$jsonp = json_encode($arr);
//header('Content-Type: application/json;');
echo($_GET['callback']."(".$jsonp.");");
?>


también he probado

Código: javascript
$http.jsonp('http://127.0.0.1:8080/llantas/index.php?name=texto&callback=JSON_CALLBACK')......


también

Código: javascript
$http({method:'JSONP',url:'http://127.0.0.1:8080/llantas/index.php?name=texto&callback=JSON_CALLBACK'})...


y siempre recibiendo un error 404-undefined y he probado haciendo la peticiendo en un script normal fuera de ionic con xamp y haciendo la petición ajax y si funciona, también mediante el navegador y también funciona y ya no se que puede estar pasando tambien tengo estas dos lineas

config.xml

Código: xml
<access origin="*"/>
<allow-navigation href="http://*/*"/>


No se si alguien de ustedes trabaja con angularjs o con ionic pero creo que he agotado todas las alternativas y no consigo hacerlo funcionar, al principio pense que podría ser ionic al usar $http pero después añadí la librería jquery e hice la petición directamente con ajax como podeis ver y descarte que fuera angularjs creo que algo de el código esta bloqueando las peticiones al exterior pero me estoy volviendo loco intentando saber que es porque en la documentación se ve practicamente fácil.

Cuando usas $http.* el siempre hace una petición OPTIONS antes de enviar la petición real, revisa que puede ser eso... yo redirecciono las peticiones options a un 200 OK para que no haya problema.

No tienes permitido ver los links. Registrarse o Entrar a mi cuenta

Acepta con humildad y aprecio que en la vida la muerte es inevitable y amarás ésta, adorando la muerte

El peoblema estaba en esta linea

Código: html5
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' ">


Solo lo cambie por

Código: html5
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval' http ">


Y ahora me funcionan todas las peticiones http tambien pudiendo cambiar http por pa url de el dominio que voy a utilizar