0

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

1 Answer 1

1

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).")";
Sign up to request clarification or add additional context in comments.

2 Comments

one more doubt, the data that I will be receving will be a javascript code. So is there need to define dataType ?
yes you need. Actually you should receive the data as json, which is a subset of javascript, not 100%javascript and you should export it from php using json_encode.

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.