When it comes to CORS, we consider JSONP also. This article will take you through each and every detail of JSONP and most importantly, why NOT to use them. Before going any further, I will start with the explanation of technical terms used here so that you don’t need any other reference material.
What is CORS?
CORS is acronym of Cross Origin Resource Sharing. Broadly, it means getting the data from different Web Servers. Two URLs are said to be from the same origin when they have identical schemes, hosts, and ports.
e.g.
- http://example.com/foo.html
- http://example.com/bar.html
But, these following URLs are NOT from same origin and any data sharing among them will come under Cross Origin Resource Sharing (CORS).
- http://example.net - Different domain
- http://example.com:9000/foo.html - Different port
- https://example.com/foo.html - Different scheme
- http://www.example.com/foo.html - Different subdomain
Getting more basic, when you create two projects in one solution, one is Web API and another is Web Client project, they will also be in Cross Origin as their URLs will be something like this,
- http://localhost:54150/
- http://localhost:54154/
So, to communicate between them, we will have to EnableCORS (How to do that is not within the scope of this article :P ).
I hope I was able to explain the concept of CORS properly.
What is JSONP?
JSONP means JSON with Padding and JSON stands for Java Script Object Notation. Exact definition of JSONP goes like this.
Not very clear ? Don’t worry ! Even I couldn’t understand much with this but focus on the 3 groups of 3 magical words underlined above. I will come back to them again to show you the actual magic.
How JSONP Works?
Don’t panic. I promise after these few lines, you will understand actually what JSONP is and how it works.
In your web page, you have seen many references to different CDNs for jQuery, AngularJS, and/or many JS frameworks, like this.

Some of them are to Google, Microsoft and others. Of course, they are NOT in the same origin you are requesting from and we don’t get any error in downloading those files. Right? So, it means, in script tag, you can have CORS without any issue and JSONP takes the benefit.




Join the conversation! Your thoughts help the community grow.