1

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.

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
Allen
  • 3,601
  • 10
  • 40
  • 59

2 Answers2

1

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

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • this doesn't work at the moment. still responds with no data :( – Allen Apr 09 '13 at 08:01
  • error: function(XHR, textStatus, errorThrown){ alert("ERREUR: " + textStatus); alert("ERREUR: " + errorThrown); } replace error function with this and see what is the error message and add in your question – M Khalid Junaid Apr 09 '13 at 08:04
  • I'm getting a "ERREUR: parsererror" followed by "ERREUR: Error: jQuery191023364612134173512_1365495173377 was not called". Appreciate your help so far! – Allen Apr 09 '13 at 08:13
  • http://stackoverflow.com/questions/5095307/jquery-json-response-always-triggers-a-parseerror http://stackoverflow.com/questions/5359224/parsererror-after-jquery-ajax-request-with-jsonp-content-type see these answers may it will solve your issue – M Khalid Junaid Apr 09 '13 at 08:35
1

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.

Fr0g
  • 252
  • 1
  • 9
  • oh, I missed the datatype parameter, change it to json, replace" dataType: 'jsonp'" with "dataType:'json'" – Fr0g Apr 09 '13 at 11:40