On May 25, 2015, at 4:15 PM, Taras Matsyk <li...@ruby-forum.com> wrote:

> Great, it did the trick.
> 
> Thank you, Walter.
> 
> Just one more question, I started googling for differences between JSON 
> (used in my example) and JSONP format suggested by you. I would 
> appreciate if you could give a couple of links or resources to read more 
> about JSONP, CORS or anything like that so I can get better 
> understanding of why my example does not work and what are alternatives 
> or "better ways" of implementing rails API.

The distinction comes into play when you are using JavaScript to parse the 
reply and update the page. If you are using a JSON API in another Rails 
application (or PHP, or any other server-side language) then you don't need to 
worry about this at all. 

JavaScript interpreters all enforce a Same Origin policy, which says that a 
script can only access data from a site that is in the same domain, subdomain, 
and port as itself. 

When you control both endpoints, and they don't pass the Same Origin tests, and 
you don't want to set up CORS, JSONP allows you to create a callback function 
to modify the page based on "foreign" JSON content. If you control both 
endpoints, and you want to set up CORS, then a traditional JSON reply is nicer, 
because you only rely on that endpoint to send you well-formed data, nothing 
else. JSONP exposes your implementation details to the API sender, and counts 
on that sender to reply with the callback that you indicated in your request 
within the body of the JSON response.

The HTML site containing these lines: 
https://github.com/walterdavis/api/blob/master/index.html#L117-L130 shows off 
both JSON (CORS) and JSONP (no CORS needed -- although my example Rails app has 
rack-cors installed -- it worked before I did that). Those lines are the 
entirety of the JSONP scheme -- one function, which takes the JSON reply from 
the server as its only argument, and one injected script tag to kick it all off.

Walter

> 
> -- 
> Posted via http://www.ruby-forum.com/.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-talk+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/fbfbc179f0806a619f8ec053f1c10e4c%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/A95F7F98-8D1B-4846-899F-2CA8C0E021FD%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to