Last active
December 11, 2015 15:38
-
-
Save naokazuterada/4622209 to your computer and use it in GitHub Desktop.
This file contains 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
$(function(){ | |
// #url is textfield | |
$("#url").change(function(){ | |
var url = '/get_page?url='+$(this).val(); | |
$.ajax({ | |
url: url, | |
cache: false, | |
dataType: 'html', | |
success: function(html){ | |
var title = html.match("<title>(.*?)</title>")[1]; | |
console.log(title); | |
} | |
}); | |
}); | |
}); |
This file contains 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
require 'net/http' | |
require 'uri' | |
class GeneralController < ApplicationController | |
def index | |
end | |
# For ajax error like "Origin http://localhost is not allowed by Access-Control-Allow-Origin." | |
def get_page | |
url = URI.parse(params[:url]) | |
req = Net::HTTP::Get.new(url.path) | |
res = Net::HTTP.start(url.host, url.port) {|http| | |
http.request(req) | |
} | |
headers['Access-Control-Allow-Origin'] = 'http://localhost:3000/' | |
headers['Access-Control-Request-Method'] = '*' | |
render :text => res.bod | |
end | |
end |
This file contains 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
match 'get_page' => 'general#get_page', :via => 'get' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ajax call to external url on Rails.