Home / Beginners / How to access JSON data over cross domain

How to access JSON data over cross domain

JSON is the new trend in the API world being practiced and implemented because of the usability, security and cross platform support just like XML. But one aspect in which JSON always outsmarts XML is the size of the response and the request.

As JSON generated at the endpoint API can be accessed by everyone over the globe, by default JSON cannot be shared on different networks or domain. There is a trick need to be done in the .htaccess file. You call this CORS (Cross Origin Resource Sharing). Just add the following line to your .htaccess file and you are good to go.

Header set Access-Control-Allow-Origin, "*"

The above code is too open. Any one over the internet will have access to your data if you use *. If you just want to provide access only to a specific set of websites or just one, try the following method.

Header set Access-Control-Allow-Origin: http://domain1.com
Header set Access-Control-Allow-Origin: http://domain2.com
Header set Access-Control-Allow-Origin: http://domain3.com
Header set Access-Control-Allow-Origin: http://domain4.com

I hope this tip helped you solve.

About Ashik

I am a Full Stack Developer and love to work on APIs and Apps. Hardcore lover of Ionic and Laravel <3

Check Also

Get started with basic form design and validation using jQuery – #1

As this being the first tutorial for beginners, let us see how to design a …

Do you wan't to get notified?

I will not spam your inbox. I will only send email whenever I publish a new article or to share information about technology

You have Successfully Subscribed!