4

I am executing an index.html file from the server test1.com. Mootools library file is included in this index.html file.

Following is a script that calls a PHP page:

<script>
  var request = new Request.JSON({
    url: 'http://test1.com/ajaxtest.php',
    onSuccess: function(data) {
      // the request was completed.
      alert(JSON.stringify(data));
    }
  }).send();
</script>

ajaxtest.php

<?php
  $arr['age'] = 30;
  $arr['place'] = 'London';
  echo json_encode($arr); exit;
?>

While executing index.html, I'm getting the correct output"

{"age":30,"place":"London"}

Now, ajaxtest.php is residing on another server, say test2.com. How to change the above script to make it work as earlier?

dda
  • 6,030
  • 2
  • 25
  • 34
user2659554
  • 113
  • 2
  • 10
  • 1
    Are you looking for [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy)? Why doesn't it work as earlier when residing on `test2.com`, what errors are you getting (set up an error handler!)? – Bergi Oct 09 '13 at 12:38
  • Is he other server set up to handle CORS? – epascarello Oct 09 '13 at 12:48
  • @epascarello. I have added the code header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']); header('Access-Control-Allow-Methods: POST, GET, OPTIONS'); header('Access-Control-Max-Age: 1000'); header('Access-Control-Allow-Headers: Content-Type'); in ajaxtest.php – user2659554 Oct 09 '13 at 12:54
  • 2
    possible duplicate of [javascript | Mootools vs native javascript](http://stackoverflow.com/questions/18445337/javascript-mootools-vs-native-javascript) – Dimitar Christoff Oct 10 '13 at 14:54

1 Answers1

2

Not sure if this will be helpful to you now.

You need to use the Request.JSONP Class object to make cross site request:

new Request.JSONP({
url: "http://search.twitter.com/search.json",
data: {
    q: "Arsenal"
},
onComplete: function(tweets) {
    // Log the result to console for inspection
    console.info("Twitter returned: ",tweets);
}
}).send(); 
akmsharma
  • 151
  • 1
  • 1
  • 7