Skip to content

[jQuery.ajax about jsonp] error description for dataType and more explicit description for url #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Aqours opened this issue Jul 22, 2016 · 9 comments

Comments

@Aqours
Copy link

Aqours commented Jul 22, 2016

URL: http://api.jquery.com/jQuery.ajax/

  1. An error description for dataType
    qq 20160722102217
    Actually cross-domain "json" requests will be not converted to "jsonp".
  2. JavaScript code
// the url of current page is
// http://www.a.com
// then send a request
// http://www.b.com/search.php support jsonp
$.ajax({
    url: "http://www.b.com/search.php?callback=?",
    data: {
        // ...
    },
    dataType: "json"
});

This request will be auto converted to "jsonp". Write this feature on jQuery document that will be good for users. I think this way is more useful than dataType: "jsonp" occasionally.

@dmethvin
Copy link
Member

Doesn't your example show the documentation is correct?

@Aqours
Copy link
Author

Aqours commented Jul 25, 2016

  1. The wrong on jQuery document.
  2. Adds an extra "?callback=?" to the end of your URL to indicate that loads in a JSON block using JSONP when using dataType: "json" or dataType: "jsonp". (I think it will be a more accurate description about my example)

@onebytegone
Copy link
Contributor

I recently noted this issue as well. The section that is particularly confusing is listed below, please note bold italicized text.

"json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests are converted to "jsonp" unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

From this section of the docs I would gather that, using the example that @Aqours gave, the following would result:

  1. The script loads on the domain of 'https://www.a.com' in a browser does not have CORS, e.g. IE9.

  2. An ajax request is made to a different domain using the json data type.

    $.ajax({
       url: 'https://www.b.com/search.php',
       dataType: 'json'
    });
  3. jQuery appends a callback parameter to the url and makes the call via the script transport

    • This assumption is based on the following:
      • The request is cross domain ('https://www.b.com' !== 'https://www.a.com')
      • 'jsonp' !== false as the jsonp option defaults to callback
      • The dataType is json and according to "Cross-domain "json" requests are converted to "jsonp"" will be treated as JSONP

The above however does not occur, no callback is appended nor is the <script> transport used. In order for the request to be treated as JSONP, that of the url receiving a callback function and being request via the script tag transport, one of the following must happen:

// Option 1: `callback=?` in url
$.ajax({
   url: 'https://www.b.com/search.php?callback=?',
   dataType: 'json'
});

// Option 2: `dataType` set to `jsonp`
$.ajax({
   url: 'https://www.b.com/search.php',
   dataType: 'jsonp'
});

In light of this, the following seems more accurate:

"json": Evaluates the response as JSON and returns a JavaScript object. Cross-domain "json" requests that have a callback placeholder, e.g. ?callback=?, are performed using JSONP unless the request includes jsonp: false in its request options. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

A more extensive set of examples of this behavior can be found at: https://github.com/onebytegone/jquery-ajax-jsonp

Note: This behavior has been confirmed in jQuery v1.11.0, v1.12.4, v2.2.4, and v3.1.1.

@dmethvin
Copy link
Member

This sounds like a very good clarification, can you submit a PR at https://github.com/jquery/api.jquery.com/?

The current docs are consistent in that dataType is about the type of the received data and it only mentions changing the URL when it does so itself. However, I can understand the confusion.

@AurelioDeRosa
Copy link
Member

@dmethvin are you sure you wanted to close this issue?

@dmethvin dmethvin reopened this Jan 27, 2017
@dmethvin
Copy link
Member

Duh, no! I must have confused two tabs at the time. @onebytegone you are in the right place! 😕

@mgol
Copy link
Member

mgol commented Feb 11, 2018

@Aqours Why did you close it? Reopening.

@mgol mgol reopened this Feb 11, 2018
@Aqours
Copy link
Author

Aqours commented Feb 12, 2018

@mgol For a long time no one had dealt with this issue. Remain issue opened if you track it.

@mgol
Copy link
Member

mgol commented Feb 12, 2018

@Aqours All of the people working on this repo are volunteers and we have limited manpower so there are silent periods.

mgol pushed a commit that referenced this issue Mar 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants