Posted in

Enviar datos y recoger respuesta desde PhoneGap a una API externa

Hay que tener en cuenta que añadiremos como dataType “jsonp” ya que se trata de un crossdomain.
PhoneGap

$("#form").submit(function(e){
        e.preventDefault();

        $.ajax({
            type       : "GET",
            url        : "http://smartparking.dev/api/v1/get",
            crossDomain: true,
            beforeSend : function() {},
            complete   : function() {},
            data       : {
                api_token_id : '123', 
                api_token_key : '1234'
            },
            dataType   : 'jsonp',
            success    : function(response) {
                console.log(JSON.stringify(response));
            },
            error      : function() {
                console.error("error");              
            }
          }); 
    });

La aplicación, al tratarse de un crossdomain devuelve la respuesta usando Jsonp (añadiendo setCallback)

return Response::json(array('name' => 'Steve', 'state' => 'CA'))->setCallback(Input::get('callback'));

Deixa un comentari

L'adreça electrònica no es publicarà. Els camps necessaris estan marcats amb *


The reCAPTCHA verification period has expired. Please reload the page.