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>
7
  • you CAN manifest a php url the same as any other, don't know what you heard... Commented Jun 17, 2013 at 21:07
  • Well, I don't know where I originally read it, but this confirms what I said: stackoverflow.com/a/3885565/2471501 Commented Jun 17, 2013 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: 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). Commented Jun 17, 2013 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... Commented Jun 17, 2013 at 21:23
  • @dandavis: You just have to configure the Ajax call properly. Commented Jun 17, 2013 at 21:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.