skip to Main Content

Hey guyz i am new to stackoverflow community.I am facing a issue regarding cross domain request. Wheni get the data from other domain i can do it using jsonp but jsonp only get the data but when i try to post data using ajax in json format it is giving me cross-domain error in console. I have read many forums some stackoverflow solutions as well but nothing worked for me.

3

Answers


  1. Browsers don’t make cross domain ajax request. It is restricted. You have to use either api or jsonp. Your question is already answered at here.

    Login or Signup to reply.
  2. You are missing the simplest piece of the PUZZLE. You do not make Ajax Requests to your very own server from a Shopify store without using the App Proxy. If you have installed an App in the store, then you can use the App Proxy. Using the App Proxy, you can securely POST/GET to your endpoint at your domain, without CORS or that shockingly bad JSONP crap.

    TL:DR; all your problems with Ajax disappear when you use the App Proxy.

    Login or Signup to reply.
  3. I do this “sort of” API calls, not product create just getting info, on a separate app on Heroku. Sinatra works fine.

    Assuming you are using Ruby/Sinatra, do something like this on your Heroku/Whatever App:

        configure do
         $apikey = ENV['API_KEY']
         $password = ENV['PASSWORD']
         $shopname = ENV['SHOPNAME']
        end
    
    post '/somepath' do
      puts params.inspect
      ShopifyAPI::Base.site = "https://#{$apikey}:#{$password}@#
      {$shopname}.myshopify.com/admin"
      my_variant = ShopifyAPI::Variant.find(variant_id)
      puts my_variant.inspect
      #or whatever you want to do
    end
    

    The approach is to construct your base site and then issue calls to the various API like this:

    ShopifyAPI::Product.create(:handle => 'foo', :title => 'Foo')
    

    You’ll probably need more options, just look at the JSON docs and try and map them to the Ruby API library. You can always of course use raw JSON, but you’ll need to handle the authentication which the shopify_api Ruby gem handles for you:

    https://github.com/Shopify/shopify_api

    Hope this helps.

    You definitely want to avoid CORS/JSONP, they fail behind firewalls for customers, also silently with security add-ons, all sorts of ways. Very frustrating for them. Embedded, Proxy Apps are the way to go if you need to send something from your site via straight up JSON to an external app. All you need to do is create an embedded app, very easy with the Rails Gem and not too hard with the Sinatra one, there is also sample code on the Shopify site, and make it a proxy as well.

    This means when your site submits to mystore.shopify.com/apps/some-path/some-otherpath it proxy passes through to myapp.heroku.com/some-otherpath or wherever your app is hosted.

    But if you just need to touch Shopify to get products and stick them in a database, or modify them, or create them via some outside app, the above code will work for you with obviously some modifications.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search