Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added supports for dataType:'jsonp' to $.ajax #367

Closed
wants to merge 3 commits into from

2 participants

@buger

You don't have to specify "callback=?" parameter.
Now this should work:

  $.ajax({
    url: "http://some_url/api.jsonp",
    dataType: "jsonp"
  });

This is helps when migrating from jQuery.

buger added some commits
@buger buger $.ajax now supports jsonp data type
You don't have to specify "callback=?" parameter.
Now this should work:

  $.ajax({
    url: "http://some_url/api.jsonp",
    dataType: "jsonp"
  });

This is helps when migrating from jQuery.
3f4afac
@buger buger Renamed fixture ade598f
@buger buger Fixed: Wrong url if data and callback both is not defined d6a1e99
@mislav mislav was assigned
@mislav mislav closed this pull request from a commit
@mislav mislav ajax: add support for 'jsonp' dataType
Closes #367
7572f37
@mislav mislav closed this in 7572f37
@mislav
Collaborator

Thanks. Since few internals of the ajax function changes since you wrote this code, I implemented this a bit differently myself.

@lopper lopper referenced this pull request from a commit in buddydvd/zepto
@mislav mislav ajax: add support for 'jsonp' dataType
Closes #367
7abed90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 8, 2012
  1. @buger

    $.ajax now supports jsonp data type

    buger authored
    You don't have to specify "callback=?" parameter.
    Now this should work:
    
      $.ajax({
        url: "http://some_url/api.jsonp",
        dataType: "jsonp"
      });
    
    This is helps when migrating from jQuery.
  2. @buger

    Renamed fixture

    buger authored
  3. @buger
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 1 deletion.
  1. +11 −1 src/ajax.js
  2. +16 −0 test/ajax.html
  3. +1 −0  test/fixtures/jsonp_data_type.js
View
12 src/ajax.js
@@ -210,7 +210,17 @@
if (!settings.crossDomain) settings.crossDomain = /^([\w-]+:)?\/\/([^\/]+)/.test(settings.url) &&
RegExp.$2 != window.location.host;
- if (/=\?/.test(settings.url)) return $.ajaxJSONP(settings);
+ if (/=\?/.test(settings.url)) {
+ return $.ajaxJSONP(settings);
+ } else if (settings.dataType && settings.dataType.match(/jsonp/i)) {
+ if (settings.url.match(/\?.*=/)) {
+ settings.url += '&callback=?'
+ } else {
+ settings.url += '?callback=?'
+ }
+
+ return $.ajaxJSONP(settings);
+ }
if (!settings.url) settings.url = window.location.toString();
if (settings.data && !settings.contentType) settings.contentType = 'application/x-www-form-urlencoded';
View
16 test/ajax.html
@@ -119,6 +119,22 @@
t.assertIn('abort', xhr);
},
+ testAjaxWithJSONPDataType: function(t){
+ t.pause();
+ var xhr = $.ajax({
+ url:'fixtures/jsonp_data_type.js?timestamp='+(+new Date),
+ dataType: "jsonp",
+ success: function(data){
+ deferredResume(t, function(){
+ this.assertEqual(data.items.length, 0);
+ this.assertEqual(0, $('script[src^=fixtures]').size());
+ });
+ }
+ });
+ t.assertEqual(1, $('script[src^=fixtures]').size());
+ t.assertIn('abort', xhr);
+ },
+
testAjaxLoad: function(t) {
var testEl = $('#ajax_load');
t.pause();
View
1  test/fixtures/jsonp_data_type.js
@@ -0,0 +1 @@
+jsonp2({"items":[]})
Something went wrong with that request. Please try again.