Loren
Loren

Reputation: 14866

How do I circumvent the same origin policy using JSONP and JQuery?

A page served from one.com has the following code:

  $.post 'http://two.com/',
    data: 'example'
  , dataType: 'jsonp'

It gives the following console error:

XMLHttpRequest cannot load http://two.com. Origin http://one.com is not allowed by Access-Control-Allow-Origin. 

Upvotes: 0

Views: 205

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074495

JSONP is, by its very nature, a GET, not a POST. It uses a script tag as its transport mechanism, and script tags GET their scripts.

$.get 'http://two.com/',
  data: 'example'
, dataType: 'jsonp'

Also note that the server has to support JSONP for it to work, just like it would have to support (say) XML if you were requesting that. The format of what it sends back is specific to JSONP.

Upvotes: 1

Related Questions