Allen
Allen

Reputation: 3771

Phonegap AJAX call fails

I'm trying to get a simple phonegap app to communicate with my server.

This is my javascript:

<script src="js/jquery.min.js" ></script>
    <script>
        $.ajax({
            url: 'http://www.drimmit.com/test',
            dataType: 'jsonp',
            jsonp: 'jsoncallback',
            timeout: 5000,
            success: function(data, status){
                //data loaded
                alert(data);
            },
            error: function(){
                //error loading data
                alert('no data');
            }
        });
    </script>

I've also whitelisted the domain in the config.xml:

<access origin="http://www.drimmit.com" subdomains="true"/>

On my server, I'm doing a simple echo statement.

<?php
header('Content-type: application/json');
echo 'hello from server';

?>

In the end, I just get a popup (iOS) saying "no data", meaning it failed.

Upvotes: 1

Views: 5515

Answers (2)

Fr0g
Fr0g

Reputation: 252

you have mentioned the request type as jsonp, but from server you are rendering json, hence the issue, if u remove the "jsonp: 'jsoncallback'" statement from your ajax call , it should work fine, also replace dataType:'jsonp' with dataType:'json'.

PS - jsonp is used if you are making cross doamin requests, but in phonegap it is not required as you have already whitelisted the domain.

Upvotes: 1

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

You have mentioned the dataType: 'jsonp' in response you are sending the string

<?php
header('Content-type: application/json');
echo json_encode('hello from server');
?>

Hope this helps

Upvotes: 1

Related Questions