Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:51:54+00:00 2026-05-26T20:51:54+00:00

I’m not sure that I really understand how Sinatra works. I’d like to get

  • 0

I’m not sure that I really understand how Sinatra works.

I’d like to get some products from Amazon using their API, in my Rails app. But HTTP requests are blocking the IO. I got the tip to create a Sinatra app and make an Ajax request to there instead.

Ajax: (From my Rails app)

$.ajax({
  url: "http://sinatra.mydomain.com",
  dataType: "json",
  success: function(data) {
    console.log(data);
  }
});

Sinatra app: (I also make use of the Sinatra-synchrony gem)

require 'sinatra'
require 'sinatra/synchrony'
require 'erb'
require 'rest-client'
require 'amazon_product'

Sinatra::Synchrony.overload_tcpsocket!

get '/' do  
  req = AmazonProduct["us"]
  req.configure do |c|
    c.key    = "KEY"
    c.secret = "SECRET"
    c.tag    = "TAG"
  end
  req << { :operation    => 'ItemSearch',
           :search_index => "DVD",
           :response_group => %w{ItemAttributes Images},
           :keywords => "nikita",
           :sort => "" }
  resp = req.get
  @item = resp.find('Item').shuffle.first

  erb :layout, :locals => { :amazon_product => @item }
end

Layout.erb: (renders fine if I go to this Url in the browser)

<%= amazon_product %>

Problem:

My Ajax response is a 200 OK but with an empty response.
I’m can’t figure out what’s wrong. Please advise.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:51:55+00:00Added an answer on May 26, 2026 at 8:51 pm

    It seems that you’ve faced with ajax ‘cross-domain security’ problem. Try to use JSONP (JSON with padding).

    Change your sinatra get handler:

    get '/' do  
      req = AmazonProduct["us"]
      req.configure do |c|
        c.key    = KEY
        c.secret = SECRET
        c.tag    = TAG
      end
      req << { :operation    => 'ItemSearch',
               :search_index => "DVD",
               :response_group => %w{ItemAttributes Images},
               :keywords => "nikita",
               :sort => "" }
      resp = req.get
      @item = resp.find('Item').shuffle.first
    
      content_type :json
      callback = params.delete('callback') # jsonp
      json = @item.to_json
    
      if callback
        content_type :js
        response = "#{callback}(#{json})" 
      else
        content_type :json
        response = json
      end
      response
    end
    

    And change your Ajax request:

    $.getJSON("http://address_of_sinatra?callback=?",
      function(data) {
        console.log(data);
    });
    

    Or you can add dataType: 'jsonp' to your $.ajax request.
    After that you should see data object in js debugger (at least it’s working in my case 😀 )

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.