Skip to content

Instantly share code, notes, and snippets.

@naokazuterada
Last active December 11, 2015 15:38
Show Gist options
  • Save naokazuterada/4622209 to your computer and use it in GitHub Desktop.
Save naokazuterada/4622209 to your computer and use it in GitHub Desktop.
$(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);
}
});
});
});
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
match 'get_page' => 'general#get_page', :via => 'get'
@naokazuterada
Copy link
Author

Ajax call to external url on Rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment