0

I have been following the tutorial on http://wiki.phonegap.com/w/page/42450600/PhoneGap%20Ajax%20Sample to build a simple AJAX request. But where I expect to see results all I get is a blank white page.

<!DOCTYPE html>
<html>
    <head>
        <title>PhoneGap Ajax Sample</title>
        <script type="text/javascript" src="phonegap.js"></script>
        <script type="text/javascript">
            function appReady(){
                var ajax = new XMLHttpRequest();
                ajax.open("GET","http://search.twitter.com/search.json?q=bacon",true);
                ajax.send();

                ajax.onreadystatechange=function(){
                    if(ajax.readyState==4 && (ajax.status==200||ajax.status==0)){
                        eval('var data = ' + ajax.responseText + ';');
                        var theResults = data.results;
                        alert(theResults);
                        var theHTML = '';
                        for(var i=0;i<theResults.length;i++){
                            theHTML += ['<div class="tweet"',
                                            '<div class="avatar"> <img src='+theResults[i].profile_image_url+' /></div>',
                                            '<div class="tweet_content">',
                                                '<h2>'+theResults[i].from_user+'</h2>',
                                                '<p>'+theResults[i].text+'</p>',
                                            '</div>',
                                        '</div>'].join('');
                        }
                        document.getElementById('main').innerHTML = theHTML;
                    }
                }
            }
            document.addEventListener("deviceready", appReady, false);

        </script>

        <style type="text/css">
            .tweet {padding-bottom:5px;}
            .avatar {float: left; height: 48px; width: 48px;}
            .tweet_content {margin-left: 60px; min-height: 48px;}
        </style>
    </head>
    <body>
        <div id="main">

        </div>
    </body>
</html>

Any ideas? I have looked all around and heard Cordova.plist this and External Hosts that, but cannot find where to add the whitelisted server...

Any help would be very grateful!

1 Answers1

0

You have give permission to access the another website inside your application. just change the cordava.xml like this. To access twitter,

<?xml version="1.0" encoding="utf-8"?>
<cordova>
    <access origin="http://search.twitter.com/"/>
    <log level="DEBUG"/>
</cordova>

To Access all websites.

<?xml version="1.0" encoding="utf-8"?>
<cordova>
    <access origin="*"/>
    <log level="DEBUG"/>
</cordova>
Akilan
  • 1,707
  • 1
  • 16
  • 30