Side navigation
#13698 closed bug (notabug)
Opened March 31, 2013 10:04AM UTC
Closed April 05, 2013 08:42AM UTC
Last modified April 05, 2013 11:39AM UTC
Param value "jsonp:false" of method $.ajax() not working
Reported by: | GolubevS79@gmail.com | Owned by: | GolubevS79@gmail.com |
---|---|---|---|
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.
Attachments (0)
Change History (5)
Changed April 04, 2013 07:55PM UTC by comment:1
cc: | → jauborg |
---|
Changed April 04, 2013 08:34PM UTC by comment:2
owner: | → GolubevS79@gmail.com |
---|---|
status: | new → pending |
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?
Changed April 05, 2013 07:42AM UTC by comment:3
status: | pending → new |
---|
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
Changed April 05, 2013 08:42AM UTC by comment:4
_comment0: | If you don't provide jsonpCallback, how will jQuery now he name of the callback 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. → 1365151413184791 |
---|---|
resolution: | → notabug |
status: | new → closed |
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.
Changed April 05, 2013 11:39AM UTC by comment:5
Oh.. I suddenly realized.. Thanks for the example.
Julian, another one that could use your touch.