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.
-
1https://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript | https://stackoverflow.com/questions/3877309/submit-cross-domain-ajax-post-request | https://stackoverflow.com/questions/30984871/cross-domain-ajax-form-post-how-is-this-allowed | https://stackoverflow.com/questions/29322999/simple-ajax-post-with-cross-domain-request – Ranjeet Singh May 27 '17 at 09:05
-
Hey Ranjit any of the solution worked for you ? – Developer Vise May 27 '17 at 09:16
3 Answers
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.

- 11
- 1
-
Thanks Floyd for your answer may be your code is working but i am php developer. I am not using Sinatra. I am using php. – Developer Vise Jun 08 '17 at 09:04
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.

- 29
- 4
-
but by using api you can get data is there any way i can post it. For instance take my situation. I am currently creating an app in shopify when i can use i get all products but when i post data to create product i am getting cross-domain error. – Developer Vise May 27 '17 at 09:25
-
I explain you create an API that will provide cross domain users data in json. These cross domain users will consume your api to get your data. I think shopify is in php built, so you create api in php then your cross domain users can consume your api in jquery or php or asp or any other language. – johnpro May 27 '17 at 09:30
-
Shopify is made in ROR framework . The thing is this is the 3rd time i am facing the issue. There are lot of apps and websites are doing same thing. For example : An app called oberlo in shopify doing exactly same thing i want to do. Take products using Aliexpress api and fetch it on there store then use shopify admin api to create product in shopify store from the app. HOw they are posting data cross-domain. – Developer Vise May 27 '17 at 09:42
-
@DeveloperVise.. Give NodeJs and Back4App/Heroku a spin.. Or even a shell program.. – HymnZzy May 27 '17 at 16:37
-
I haven't use Nodejs yet but still if it is a js i will face this issue again of crossorigin scripts... – Developer Vise May 29 '17 at 08:22
-
-
The answer is simple. In any scripting language you care to use, make an authenticated POST to the API endpoint for product create, and your product will be created. – David Lazar Jun 06 '17 at 14:14
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.

- 10,865
- 3
- 25
- 38
-
Hey David, thanks for the reply actually i am not making request from shopify to my app. I am making request from my app to shopify admin api to create product This is what i am trying to do .Click here https://help.shopify.com/api/reference/product#create – Developer Vise Jun 01 '17 at 11:35
-
-
-
Well if you are making an authenticated POST to Shopify from your App, CORS/JSONP should never come into play so that is the answer to that. Creating a Product is a simple POST of JSON to Shopify. No more, no less, and has nothing to do with XHR or other problems you point out. – David Lazar Jun 06 '17 at 13:49
-
So what is the issue according to shopify api when i post as they say in their api https://help.shopify.com/api/reference/product#create – Developer Vise Jun 08 '17 at 08:50
-
You are making an XHR call when you should just be making a plain POST. – David Lazar Jun 08 '17 at 18:48