Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ $ jspm install npm:jsonp-promise
is emitted. `0` to disable (defaults to `15000`)
- `prefix` (`String`) prefix for the global callback functions that
handle jsonp responses (defaults to `__jp`)
- `data` (`Object`) additional parameters for request (encoded as a part
of URL), optional

Returns an object containing two properties:
- `promise` a promise which will be resolved if the jsonp request succeeds,
Expand Down
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var count = 0;
* - param {String} qs parameter (defaults to `callback`)
* - timeout {Number} how long after the request until a timeout error
* is emitted (defaults to `15000`)
* - data {Object} additional parameters for request (encoded as a part of URL)
*
* @param {String} url
* @param {Object} options optional options
Expand All @@ -23,6 +24,7 @@ var jsonp = function(url, options) {

var prefix = options.prefix || '__jp';
var param = options.param || 'callback';
var data = options.data || {}
var timeout = options.timeout ? options.timeout : 15000;
var target = document.getElementsByTagName('script')[0] || document.head;
var script;
Expand All @@ -35,6 +37,17 @@ var jsonp = function(url, options) {
// Generate a unique id for the request.
var id = prefix + (count++);

// add querystring
data[param] = id;

var query = '';
for (var key in data) {
query += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
}

url = (~url.indexOf('?') ? '' : '?') + query;
url = url.replace('?&', '?');

cleanup = function() {
// Remove the script tag.
if (script && script.parentNode) {
Expand All @@ -61,10 +74,6 @@ var jsonp = function(url, options) {
resolve(data);
};

// Add querystring component
url += (~url.indexOf('?') ? '&' : '?') + param + '=' + encodeURIComponent(id);
url = url.replace('?&', '?');

// Create script.
script = document.createElement('script');
script.src = url;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonp-promise",
"version": "0.1.2",
"version": "0.1.3",
"description": "Jsonp es6 implementation that returns a promise.",
"main": "index.js",
"keywords": [
Expand Down