Skip to main content

One post tagged with "mvc"

View All Tags

Asp.net MVC 2 with JSONP and jQuery

Greg Roberts

Greg Roberts

Greg Roberts

I’m willing to bet as a web developer there will be some project that you are working on that requires you to expose your data and or services across domain boundaries through an REST API and want them to be called via javascript. Maybe you wrote a jQuery plugin that retrieves data from your site, or maybe you have several domains that share the same services. Whatever the reason is, you will quickly discover the browser same-origin policy, which states “a web page served from domain1.com cannot normally connect to or communicate with a server other than domain1.com”.

Fortunately there is a way around this. It’s called JSONP or “JSON with padding”. To summarize, JSONP works around the cross-domain browser restriction by making a GET request using a script tag, which isn’t limited by the same-origin policy. It traditionally will also provide a callback method in the url so the response from the server will look something like this:

callback({ title: "hello world!", rank: "number 1" });