0

I'm using JSONP to dynamically add data into my Android Phonegap app. Since Phonegap 2.5 (and upwards) allows application cache, I would like to use that. Only problem is that at this moment, my data is in a php-file. I read that data from a php-file cannot be cached by the cache manifest, so I'm thinking about changing it to js or something. Any idea how I would do this? I already tried a lot of tutorials on JSONP, but the only way I can get JSONP to work is in PHP. They are also quite vague about how I should save my data file (currently called home.php).

home.php

<?php echo $_GET["callback"] ?> (
[
    {
    "expo":"pit",
    "datum":"05.06.2011 - 05.06.2016",
    "img":"images/pit_home.jpg",
    "link":"exp1_index.html"
    },
    {
    "expo":"Space Odessy 2.0",
    "datum":"17.02 - 19.05.2013",
    "img":"images/so_home.jpg",
    "link":"exp2_index.html"
    }
]
);

script in index.html that calls data from home.php

<script type="text/javascript">
$.ajax({
type: 'GET',
jsonpCallback: 'jsonCallback',
contentType: 'application/json',
dataType: 'jsonp',
url: 'http://mllsdemode.be/Ex-cache/home.php',
success: function(json) {
var $home = $("#home");
$home.empty();
$.each(json, function(i, el) {
    $home.append("<td><a href=" + el.link + " data-ajax='false'><img src=" + el.img + "><div class='dsc'>" + el.expo + "<br><em>" + el.datum + "</em></div></a></td>");
});
},
error: function() { alert("Error reading jsonP file"); }
});
</script>
user2471501
  • 143
  • 1
  • 2
  • 12
  • you CAN manifest a php url the same as any other, don't know what you heard... – dandavis Jun 17 '13 at 21:07
  • Well, I don't know where I originally read it, but this confirms what I said: http://stackoverflow.com/a/3885565/2471501 – user2471501 Jun 17 '13 at 21:20
  • What is referred to as JSONP is simply a JavaScript file with a single function call. Usually it is dynamically generated, so that the client can send the function name as parameter, but it does not have to be. You can use a fixed function name and tell jQuery to use it. Have a look at the `jsonpCallback` option: http://api.jquery.com/jQuery.ajax/. The documentation explicitly mentions: *"You may want to specify the callback when you want to enable better browser caching of GET requests."* (though I don't know if the application cache works the same way). – Felix Kling Jun 17 '13 at 21:22
  • yeah, if the remote url keeps changing, you can't cache a moving target. Since the only thing about the url that changes is ?callback, you can use the same url again and again, though you might have to move from jQuery ajax to something that doesn't generate dynamic URLs... – dandavis Jun 17 '13 at 21:23
  • @dandavis: You just have to configure the Ajax call properly. – Felix Kling Jun 17 '13 at 21:24
  • @FelixKling - good to know, glad i said "might"... – dandavis Jun 17 '13 at 21:30
  • Thanks for the comments, guys. However I still have got the feeling there's no concrete answer to my question. As I said, I'm having a hard time with transforming this php file to a working jsonp file (with JS??). I also don't know how I should save the file then. Should it be js, json, ...? – user2471501 Jun 18 '13 at 11:09

1 Answers1

0

Well, if I understood well, this is cached, you want it to be cached but not to develop? Then add some headers to develop!

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

To easily forge your JSON, you can use Simple JSON for PHP, it allows you to build complex JSON/JSONP.

Alexis Paques
  • 1,885
  • 15
  • 29