Skip to content

Instantly share code, notes, and snippets.

@yuyalush
Created June 28, 2011 16:14
Show Gist options
  • Save yuyalush/1051503 to your computer and use it in GitHub Desktop.
Save yuyalush/1051503 to your computer and use it in GitHub Desktop.
CrossDomain xhr test
Deploy
------------------------------
server1
app / controller / crosstest_controller.rb
app / view / crosstest / index.html.erb
app / view / crosstest / test.json.erb
server2
app / controller / application_controller.rb
app / controller / sample_controller.rb
app / view / sample / index.json.erb
Flow
------------------------------
+ Access to http://server1/crosstest/index
+ jQuery.get access to http://server1/crosstest/test.json
+ jQuery.get access to http://server2/sample/index.json
Result
------------------------------
XHR TEST
one:Singular sensation
two:Beady little eyes
three:Little birds pitch by my doorstep
one:111
two:222
three:333
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :allow_cross_domain_access
def allow_cross_domain_access
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Headers"] = "Content-Type"
response.headers["Access-Control-Allow-Methods"] = "PUT,DELETE,POST,GET,OPTIONS"
end
end
class CrosstestController < ApplicationController
def index
end
def test
respond_to do |format|
format.json
end
end
end
<html>
<head>
<title>CrossTest</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<h1>XHR TEST</h1>
<div id="recieved"></div>
<script type="text/javascript">
$(function(){
//SAME DOMAIN XHR
$.getJSON('test.json',function(data){
$.each(data, function(key, val){
$("#recieved").append(key + ':' + val +"<br>");
});
});
//CROSS DOMAIN XHR
$.getJSON('http://192.168.11.11:4000/sample/index.json', function(data){
$.each(data, function(key, val){
$("#recieved").append(key + ':' + val +"<br>");
});
});
});
</script>
</body>
</html>
{
"one": 111,
"two": 222,
"three": 333
}
<html><head>
<title>CrossTest</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<h1>XHR TEST</h1>
<div id="recieved">one:Singular sensation<br>two:Beady little eyes<br>three:Little birds pitch by my doorstep<br>one:111<br>two:222<br>three:333<br></div>
<script type="text/javascript">
$(function(){
//SAME DOMAIN XHR
$.getJSON('test.json',function(data){
$.each(data, function(key, val){
$("#recieved").append(key + ':' + val +"<br>");
});
});
//CROSS DOMAIN XHR
$.get('http://192.168.11.11:4000/sample/index.json', function(data){
$.each(data, function(key, val){
$("#recieved").append(key + ':' + val +"<br>");
});
});
});
</script>
</body></html>
class SampleController < ApplicationController
def index
respond_to do |format|
format.json
end
end
end
{
"one": "Singular sensation",
"two": "Beady little eyes",
"three": "Little birds pitch by my doorstep"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment