How AI is applied across API Evangelist and APIs.io. Read my AI disclosure →
API Evangelist API Evangelist
Discovery
Learnings
Guidance
Toolbox
Alignment
API Evangelist LLC

Send JSONP Cross-domain Requests

calendar_today December 16, 2013 person domain json

Cross-domain Requests In developing web applications it’s commonplace to send and receive data to APIs (Application Programmable Interfaces) that exist on other domains. This cross-domain communication is often performed directly in the browser, on the client-side using JavaScript. Cross-domain communication can be accomplished using a server-side languages like Python, Nodejs, PHP, etc., however for cases where responses from cross-domain requests are utilized in the browser, JavaScript can save time and simplify development. Addintionaly, JavaScript requests are asynchronous— meaning they can be run in parallel with other processes. For example, JavaScript requests to other domains can be processed asynchronously while the page is still loading. Same-origin Policy Same-origin policy is an important security concept in client-side programming languages including JavaScript. This policy is observed by all web browsers and limits scripts running on a site to only sending requests with the same site. Browers use a combination of port number (e.g. 80) and hostname (e.g. my.domain.com) to determine the scope of the ‘site’. There are however exceptions to the same-origin policy, including using the HTML The dynamically generated JavaScript file may look like: jsonpcallback ({ "id" : "123" , "comments" : "6" , "name" : "sample" }); The “padding” in JSONP refers to the function call that wraps the formatted JSON object. This padding isn’t always a function call. Though not common, padding could also be a variable assignment: var data = { "id" : "123" , "comments" : "6" , "name" : "sample" }; The purpose of the padding is to allow a means for the formatted data to be put to use in the application. jQuery for JSONP The jQuery JavaScript library has functions to make using JSONP a snap. jQuery’s $.ajax() function will automatically handle

open_in_new Read original post