Question: Display data from AJAX cross-domain request in Sinatra application


Display data from AJAX cross-domain request in Sinatra application

Answers 0
Added at 2016-12-20 20:12

I'm trying to develop some kind of bookmaker's site parser which grabs data from certain divs with Google Chrome extension written on JS (JQuery) and passes it via cross-domain AJAX GET request to Sinatra app deployed on localhost.

Here is the JQuery function. It works correctly and passes data to local https webserver.

function refreshScores() {
    currentScore = $('.result-description-part').eq(0).html();
    firstMatchWinner = $('.foot-market td[data-mutable-id="S1mainRow"] span').html();
    secondMatchWinner = $('.foot-market td[data-mutable-id="S2mainRow"] span').html();

    if(firstMatchWinner != prevFirstMatchWinner) {
            type: 'GET',
            url: 'https://localhost/coefs',
            crossDomain: true,
            data: { firstmatchwinnercoef: firstMatchWinner},
            success: function () {

    prevFirstMatchWinner = firstMatchWinner;

  setInterval(refreshScores, 500);


Here is the main app code

require 'sinatra'
require './sinatra_ssl'
require 'sinatra/cross_origin'
require 'socket'

configure do
  enable :cross_origin

before do
  response.headers["Access-Control-Allow-Origin"] = "*"

set :port, 443
set :ssl_certificate, "server.crt"
set :ssl_key, "server.key"

get "/coefs" do
    erb :index, :locals => {:firstmatchwinnercoef => params[:firstmatchwinnercoef]}

So, I can see both in browser with bookmaker's website and in Ruby command prompt that AJAX works correctly and passes data to https://localhost/coefs. But how can I display and manipulate this data in Sinatra application? I don't want to write it immediately to database. Should I use websockets, sessions or cookies? What approach would be the best? Any help would be appreciated, thank you!

Source Show
◀ Wstecz