Forum Moderators: open

Message Too Old, No Replies

Cross Domain jQuery JSONP Caching

         

username

9:14 am on Feb 27, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi all, I'm building an application which uses a jQuery JSONP request across domains. The functionality works fine, but I cannot get it to cache the request. I have used the following, but am wondering what's missing, or if this is not possible?

/* JAVASCRIPT */
$.ajax({
url:"http://cdn.example.com/",
type:'GET',
async: true,
crossDomain:true,
cache:true,
dataType: 'jsonp',
jsonp:'callback',
jsonpCallback:'myCallback',
data:'id='+1,
success: myCallback
});

function myCallback(json){
//do stuff
}

/* PHP */
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/javascript');
$expires = 60*60*24*14;
header("Pragma: public");
header("Cache-Control: maxage=".$expires.", public");
header('Expires:'.gmdate('D, d M Y H:i:s', time()+$expires).' GMT');

$arr = array("123");
echo "myCallback(".json_encode($arr).");";

username

10:15 pm on Feb 27, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi guys, any ideas on this issue?

daveVk

2:30 am on Feb 28, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does it cache correctly if you enter http://cdn.example.com/?id=1 into location bar ?

username

2:44 am on Feb 28, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



Interesting, it doesn't seem to. It just keeps giving a 200 OK status response. The type is set to "application/javascript". Should this be something else, such as "application/json" even though it is a PHP page echoing the content back to the JS?

daveVk

4:54 am on Feb 28, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check all headers as received by browser via browser plugin or whatever, server or middleware may effect headers, "application/javascript" probably OK.