Posted By

kendsnyder on 03/29/11


Tagged

json JSONP


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

notturnale


JSONP Request


 / Published in: JavaScript
 

Using .join('&') because with long strings it will take less processing and memory to build an array than to build a string.

  1. function doJsonp(url, data, callback) {
  2. data.callback = callback;
  3. var get = [];
  4. for (var field in data) {
  5. get.push(encodeURIComponent(field) + '=' + encodeURIComponent(data[field]));
  6. }
  7. var script = document.createElement('script');
  8. script.setAttribute('type', 'text/javascript');
  9. script.setAttribute('src', url + '?' + get.join('&'));
  10. document.head.appendChild(script);
  11. }

Report this snippet  

You need to login to post a comment.