Created
June 28, 2011 16:14
-
-
Save yuyalush/1051503 to your computer and use it in GitHub Desktop.
CrossDomain xhr test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CrosstestController < ApplicationController | |
def index | |
end | |
def test | |
respond_to do |format| | |
format.json | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"one": 111, | |
"two": 222, | |
"three": 333 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SampleController < ApplicationController | |
def index | |
respond_to do |format| | |
format.json | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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