Shades88
Shades88

Reputation: 8360

how to get a javascript code from JSONP in jquery

I have a php file, which accesses memcache and gets a stored javascript code. This file then echoes the js content. I am using iFrame to access this file. But now there is a requirement to get this JS code without using iFrame. I am thinking of making a AJAX call and get that js code. The problem is that, that php file is cross domain. I learnt that ajax can't operate cross browser. JSONP holds the answer. But I don't know syntax. I checked a lot of documents but couldn't figure out how to do it.

This is my php file memcacheJs.php:

$id        =  $_GET['mc_id'];
$js_code   =  $memcacheRW->get($id);
echo          $js_code;
$memcacheRW -> delete($id);

I have to call this file, sending mc_id, get that js_code as ajax response. I tried this code of jquery:

var jsCode      = "js_code="+_cO.cmK[keyword].ad[0][4];
var crossDomURL = "http://ph.cm.shades1ld1/frame2.php";

$pH.getJSON(crossDomURL+"&callback=?", function(data) {alert(data);});

But it's not working, what to do ? Please help

Upvotes: 0

Views: 109

Answers (1)

Elzo Valugi
Elzo Valugi

Reputation: 27886

You have to match the callback function, look at the jsonpCallback param. Here is an working example using jsonP (simplified).

$.ajax({
        url: "/myUrl.php",
        data: {
            'date' : '2011-01-01', 
            'specie' : 'cervus'
        }, 
        dataType : 'jsonp',
        jsonpCallback: 'onModify',
        success: function(data){
            console.log(data);
            return false;
         }
    });

//the php code 
$data = array('some', 'values', 'in','response');
echo "onModify(". json_encode($data).")";

Upvotes: 1

Related Questions