ykuramochi / jquery-jsonp

Automatically exported from code.google.com/p/jquery-jsonp
0 stars 0 forks source link

Version 2 has trouble with embeded calls to anything named "Success*" or "*_Success*" #22

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

1. Create a simple success callback function which calls a secondary function, 
e.g.

"success" : function(json) {
   alert("its okay!");
   TestCase1_Success(json);
   alert("its still okay!");
}

2.  Both alerts fire but function TestCase1_Success does not fire.  No errors 
anywhere in firebug, its just does not go.

3. Change the name of the function to TestCase1_zSuccess or anything else 
without Success* or *_Success* in the name.

"success" : function(json) {
   alert("its okay!");
   TestCase1_zSuccess(json);
   alert("its still okay!");
}

Now it works as expected!

What is the expected output? What do you see instead?

Function should fire, but does not.  Very odd to me.

What version of the product are you using? On what operating system?

jQuery 1.4.2 but also occurs with jQuery 1.3. 
JSONP 2.1.2 but occurs on any 2.x version.  Problem goes away with version 1.4. 
 Problem occurs with Firefox 3.6.6, IE 8 and Chrome 5.

Please provide any additional information below.

Issue is most perplexing.  Change the name and the function works fine.

Thanks,

Paul

Original issue reported on code.google.com by pauldzie...@gmail.com on 6 Jul 2010 at 5:05

GoogleCodeExporter commented 8 years ago
Very weird. Do you have a complete example somewhere? I'm wondering if we're 
not lacking some crucial context in here.

Original comment by aubourg.julian on 7 Jul 2010 at 7:38

GoogleCodeExporter commented 8 years ago
Hi Julian,

Thanks for looking into this.  I admit this sure seems weird.  It all seems to 
do with the fact that we decided to name our callback the same value as the 
name of this misbehaving function (cut and paste thing for automated tests).  
If we remove the callback and use the default or rename the callback to 
anything else, it works fine.  I'll attach a test but its located here
http://indusdemo.induscorp.com/jsonp_test1.html

Indeed, I can show the same thing using the alert function!
http://indusdemo.induscorp.com/jsonp_test2.html

function TestCase1() {
   jQuery.jsonp({
      "cache" : "true",
      "callback" : "alert",
      "callbackParameter" : "optJSONPCallback",
      "complete" : function(xOptions, textStatus) {
         alert('complete');
      },
      "data" : {"pFullText" : "Dyer Lake", "optOutPrettyPrint" : 0},
      "error" : function(XMLHttpRequest, textStatus) {
         alert('error');
      },
      "success" : function(json) {
         alert('SUCCESS but alert no work!');
      },
      "url" : "http://epadev.induscorp.com/waters10/WATERS_SERVICES.NameService"
   });
}

Hope that makes it clear. Thanks again for the attention.  Your product is 
great.  Its in use (version 1.1.4) by the EPA here if you want to add to your 
list of users:
http://www.epa.gov/waters/tools/WATERSServicesJSLib/WATERSServicesJSLib.html

Cheers,

Paul 

Original comment by pauldzie...@gmail.com on 7 Jul 2010 at 12:53

Attachments:

GoogleCodeExporter commented 8 years ago
Oh, of course! Never ever use the name of an existing function as a callback 
name: version 2.x redefines the function since it has to seeing as it doesn't 
work into an iframe anymore. It's not that the function is not called, it's 
that you call jquery-jsonp's internal generic callback instead.

Original comment by aubourg.julian on 7 Jul 2010 at 1:21

GoogleCodeExporter commented 8 years ago
I added the information in the doc for the callback parameter. No way you can 
just change the callback name so that there are no more collisions? For 
instance, do you really need to change the default?

Original comment by aubourg.julian on 7 Jul 2010 at 1:36

GoogleCodeExporter commented 8 years ago
Hi Julian,

Thanks for the quick response.  For the generic wrapper we have written around 
these calls, we planned for the possibility of asynchronous usage.  If slow 
call A fires first using default callback _jqjsp and then speedy call B fires 
using the same _jqjsp callback, won't B's callback overwrite A so that when A 
finally returns it ends up executing B's function?  I haven't tested this but 
you do mention in your new wiki comment that all this happens in the global 
scope.  

So I think the answer is that any callbacks need to be unique amongst 
themselves and they need to be named such that they do not conflict with 
anything else that happens to be in the global scope?  My first thought is to 
write some kind of generator to create unique callbacks - _jqjsp + random 
number + running sequence?  Is that something we could recommend as an 
enhancement?

Thanks,

Paul 

Original comment by pauldzie...@gmail.com on 7 Jul 2010 at 4:06

GoogleCodeExporter commented 8 years ago
You can have as many concurrent requests as you want with the same callback and 
it will work as expected.

There was an issue in IE 
(http://code.google.com/p/jquery-jsonp/issues/detail?id=21) but I had a backup 
solution for the browser anyway as I suspected there could be a problem (I 
mean, it's IE, right?).

So, to make things clear, you can have as many requests running concurrently as 
you want to, with the same callback name, without any known response order and 
everything will be called as it should.

That was the behaviour in 1.x and was a mandatory feature for any 
non-iframe-based solution... which includes 2.x ;)

Original comment by aubourg.julian on 7 Jul 2010 at 10:58