Sto cercando di creare un semplice lettore RSS con PhoneGap e jQuery. Sto seguendo questo tutorial: http://visualrinse.com/2008/09/24/how-to-build-a-simple-rss-reader-with-jquery/.Problemi con richieste ajax con un'applicazione PhoneGap
Sono riuscito a farlo funzionare bene quando provo il codice nel mio browser. Il file php recupera il feed e lo emette esattamente come mi aspetto. Ma quando eseguo lo stesso file dall'applicazione Phonegap compilata, la richiesta ajax restituisce semplicemente il contenuto del file php (il codice php, non il risultato eseguito).
Ho passato ore a cercare su Google e ho provato numerosi tutorial e trucchi. Neanche ho trovato soluzioni nei forum ufficiali di PhoneGap. Che cosa sto facendo di sbagliato? Il problema sembra essere il PHP che non risponde alla richiesta. Ho provato a spostare il file php in un dominio diverso ma il risultato è lo stesso, funziona nel mio browser ma non nell'app compilata.
Ecco il codice jQuery che avvia la volta ajax-code:
function get_rss_feed() {
//clear the content in the div for the next feed.
$("#feed_content").empty().html('<img class="loader" src="js/images/ajax-loader.gif" alt=""/>');
$.ajax({
url: 'http://192.168.1.7/rssApp/www/rss-proxy.php?url=http://www.nytimes.com/services/xml/rss/nyt/GlobalHome.xml',
success: function parseRSS(d) {
//find each 'item' in the file and parse it
$(d).find('item').each(function() {
//name the current found item this for this particular loop run
var $item = $(this);
// grab the post title
var title = $item.find('title').text();
// grab the post's URL
var link = $item.find('link').text();
// next, the description
var description = $item.find('description').text();
//don't forget the pubdate
var pubDate = $item.find('pubDate').text();
// now create a var 'html' to store the markup we're using to output the feed to the browser window
var html = "<div class=\"entry\"><h2 class=\"postTitle\">" + title + "<\/h2>";
html += "<em class=\"date\">" + pubDate + "</em>";
html += "<p class=\"description\">" + description + "</p>";
html += "<a href=\"" + link + "\" target=\"_blank\">Read More >><\/a><\/div>";
//put that feed content on the screen!
$('#feed_content').append($(html));
});
$('#feed_content img.loader').fadeOut();
}
});
};
Ecco il rss-proxy.php che carica il codice XML dall'URL e l'emette:
<?php
// PHP Proxy
// Loads a XML from any location. Used with Flash/Flex apps to bypass security restrictions
// Author: Paulo Fierro
// January 29, 2006
// usage: proxy.php?url=http://mysite.com/myxml.xml
$session = curl_init($_GET['url']); // Open the Curl session
curl_setopt($session, CURLOPT_HEADER, false); // Don't return HTTP headers
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Do return the contents of the call
$xml = curl_exec($session); // Make the call
header("Content-Type: text/xml"); // Set the content type appropriately
echo $xml; // Spit out the xml
curl_close($session); // And close the session
?>
Consiglierei di rimuovere quell'indirizzo IP dal codice. – sciritai
Cosa succede quando apri il file '.php' dal browser sul simulatore o sul dispositivo? Il PHP viene eseguito? – Marko
Grazie per i commenti! Ho provato ad accedere al file '.php' dal browser nel simulatore e funziona. Ma funziona solo se cambio l'URL in modo relativo, non assoluto: 'url: 'rss-proxy.php? Url = http: //www.nytimes.com/services/xml/rss/nyt/GlobalHome.xml' '. Se ora utilizzo il browser Safari mobile per visitare il file index.html nella mia cartella www della mia applicazione Phonegap che sto ospitando sul mio server MAMP locale nella mia directory 'htdocs' funziona! Ma non dall'app Phonegap compilata. E, naturalmente, il file '.php' si trova nella cartella www insieme agli altri file di script. – user1029978