Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#13698 closed bug (notabug)

Param value "jsonp:false" of method $.ajax() not working

Reported by: GolubevS79@… Owned by: GolubevS79@…
Priority: undecided Milestone: None
Component: unfiled Version: 1.9.1
Keywords: Cc: jauborg
Blocked by: Blocking:

Description

API Documentation:

As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }

And I try to get result of:

$.ajax({
    url: "http://dvlp:40/test.aspx",
    cache: true,
    success: processJSON,
    dataType: "jsonp",
    jsonp: false,
    jsonpCallback: getJSONP
});

function getJSONP() {
    return "some";
}

but request URL is simple "http://dvlp:40/test.aspx" without any callback params.

Found following lines in the method jQuery.ajaxPrefilter() in the source script:

jsonProp = s.jsonp !== false && ( rjsonp.test( s.url ) ?
"url" :
typeof s.data === "string" && !( s.contentType || "" ).indexOf("application/x-www-form-urlencoded") && rjsonp.test( s.data ) && "data"
);  

..........................

// Insert callback into url or form data
if ( jsonProp ) {
s[ jsonProp ] = s[ jsonProp ].replace( rjsonp, "$1" + callbackName );
} else if ( s.jsonp !== false ) {
s.url += ( ajax_rquery.test( s.url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
}

but if "jsonp" equal "false", "jsonProp" equal "false" too and callback wouldn't be appended in any case.

Change History (5)

comment:1 Changed 6 years ago by dmethvin

Cc: jauborg added

Julian, another one that could use your touch.

comment:2 Changed 6 years ago by jaubourg

Owner: set to GolubevS79@…
Status: newpending

I fail to see what the problem is here:

  1. You set jsonp to false, so that no callback parameter is added to the URL and no transformation is performed on the URL
  2. You give an explicit name for the callback so that jQuery can know which function to create in order to receive the answer.

These are two orthogonal concepts.

Say you have a myStaticJSONP.js file on your server which code is:

myCallback( [ "my", "response" ] );

You'd request it as follows:

$.ajax( {
    url: "myStaticJSONP.js",
    dataType: "jsonp",
    jsonp: false,
    jsonpCallback: "myCallback"
} );

That's perfectly valid and sound.

If jsonpCallback is a function, not a string, then it is called right away and the returned value is used as the value of the option.

So the following is strictly equivalent to the previous example:

function getName() {
    return "myCallback";
}

$.ajax( {
    url: "myStaticJSONP.js",
    dataType: "jsonp",
    jsonp: false,
    jsonpCallback: getName
} );

What am I missing here? What is the behaviour you expected? Could you provide a complete client/server round-trip example?

comment:3 Changed 6 years ago by GolubevS79@…

Status: pendingnew

It seems to me if set "jsonp:false" there won't be any effect from "jsonpCallback" property. I just can't understand following lines in documentation:

As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }

Yes, it prevent. But how should I set "jsonpCallback" after that? Attempts to set property to string value or function that return string value have not led to anything

comment:4 Changed 6 years ago by jaubourg

Resolution: notabug
Status: newclosed

If you don't provide jsonpCallback, how will jQuery know the name of the callback function it needs to generate in order to receive the response.

A jsonp request is just a special kind of script that calls a function. If you wanna get the jsonp response, you need to define this function, thus you need its name. Again, it's not about the URL, it's about the recipient of the response (ie. creating the named function that will receive the response).

Please, read the example in my previous comment more carefully.

Anyway, this is obviously not a bug in jQuery.

Last edited 6 years ago by jaubourg (previous) (diff)

comment:5 Changed 6 years ago by GolubevS79@…

Oh.. I suddenly realized.. Thanks for the example.

Note: See TracTickets for help on using tickets.