Search code examples
androidajaxcordovacross-domainjsonp

cross domain ajax works in all but android


I have literally lost hair over the last 2 and a half days i've spent on this problem...

Using phonegap to build an app for iPhone and Android. Cross domain ajax works in my browser and also on iPhone. it will NOT work on Android.

I have not been using jQueryMobile, just jQuery, until i read somewhere that i should be using the mobile version to get cross domain to work on android. So i've changed up the project to suit jMobile. Do i really need to use JqueryMobile in order to get cross domain ajax working?? i would much rather not use it if possible.

tried changing things like $.mobile.allowCrossDomainPages=true; $.support.cors = true; to no avail. I'm at a loss. Please help me out

Heres my ajax code

$.ajax({
    url: 'URL',
    type: 'GET',
    contentType: "application/json",
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    success: function(response){
        console.log("inside ajax");
        $.each(response, function(i,item){
            var category = "<div class='icon-text'><a class='ajaxify' href='pages/onlineRecipe.html' onclick='$(setOnlineID("+item.id+"))' >"
                +"<p><img alt='Image-alt' width='64' class='wrap-around' id='img"+item.id+"' />"
                +"<strong>"+item.title+"</strong>"
                +"<br/>"+item.desc+"</p></a></div>";

            $('#categories').append(category);
            var img = "#img" + item.id;
            $(img).attr('src', "data:image/png;base64,"+item.image);
            //other bindings
            App.ajaxLinkify($("a.ajaxify"));
            App.addTouchEvents($(".page"));
        });
    },
    error: function(xhs, status, error){
        //output.text('Ha producido un error cargando el dato. Por favor, intèntalo de nuevo.');
        console.log("Ajax not working");
    }
});

Solution

  • need to add this to corresponding php file: header("Access-Control-Allow-Origin: *");