Last active
December 12, 2015 05:28
-
-
Save sohalloran/4722069 to your computer and use it in GitHub Desktop.
JSONPify Example - Example of adding JSONP support to a REST API via a proxy node.js service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Client JS | |
$.ajax({ | |
cache: false, | |
dataType: 'jsonp', | |
url: 'https://MY_JSONP_SERVER/jsonp?sid='+SID_HERE+'&path='+REST_RESOURCE, | |
success: function(data){ | |
console.log(JSON.stringify(data)); | |
} | |
}); | |
// Server JS | |
var jsonpHandler = function(req, res){ | |
var sid = req.query["sid"]; | |
var path = req.query["path"]; | |
rest.get(path ,{ | |
headers: { | |
'Accept':'application/json', | |
'Authorization': 'OAuth ' + sid, | |
'Content-Type': 'application/json', | |
} | |
}).on('complete', function(result) { | |
res.type('application/json'); | |
res.jsonp(result); | |
res.end(); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment