Bug Tracker

Ticket #5201 (closed enhancement: wontfix)

Opened 5 years ago

Last modified 3 years ago

getScript should support an error callback, incorrect documentation

Reported by: virajmody Owned by:
Priority: high Milestone:
Component: ajax Version: 1.4.3
Keywords: ajaxrewrite, neededdocs Cc:
Blocking: Blocked by:

Description

Currently, $.getScript doesn't accept a function that can be invoked when an error occurs. The callback it does accept is only invoked on success, and both parameters passed to it are null/undefined. Since the callback is success only, it doesn't need parameters I presume, so the documentation should be fixed. Also, an error callback provided to enable handling failure cases.

Change History

comment:1 Changed 5 years ago by robustsolution

Dear virajmody,

jQuery Allows you to return to the low level jQuery $.ajax(...) call if you need more callbacks and/or settings.

$.ajax is very simple to use.

all the high level abstraction AJAX calls like $.load(...),$.get(...), $.post(...), $.getScript(...), etc... are designed to fulfill some frequent AJAX tasks.

If you want to continue with using $.getScript(...) but with an additional callback for the error and you don't want to modify the jQuery library itself, you can use the global ajaxError callback event that has three parameter. the third one is the settings of the request that handled the error so you can test if(settings.dataType=='script' && settings.url=='...put it here') in the global ajaxError callback event to handle the getScript request that generates the error

comment:2 Changed 5 years ago by virajmody

I tried using $.ajax with dataType = "script" but that too didn't seem to invoke an error callback. I'll try the global ajaxError callback - thanks for that suggestion.

The documentation for $.getScript should be fixed regardless, though.

comment:3 Changed 5 years ago by dmethvin

  • Component changed from core to ajax

comment:4 Changed 5 years ago by ishermandom

I took a stab at adding error callbacks for remote scripts, which I assume would address this bug. See  http://github.com/ishermandom/jquery/blob/15fc751ba6b39072c9ebf2548f2deaabe3257bc0/src/ajax.js.

comment:5 Changed 4 years ago by addyosmani

I have added a reference to getScript as one of the areas of the API documentation which we need to update. Keeping this ticket open for now as there appears to have been a contribution made to address additional error callbacks that may want to be reviewed.

comment:6 Changed 4 years ago by snover

  • Keywords needsreview added

comment:8 Changed 4 years ago by snover

#4734 is a duplicate of this ticket.

comment:9 Changed 4 years ago by snover

  • Blocking 5048 added

comment:10 Changed 4 years ago by snover

  • Status changed from new to open
  • Priority changed from major to high
  • Version changed from 1.3.2 to 1.4.3
  • Milestone changed from 1.4 to 1.5
  • Keywords needsreview removed
  • Type changed from bug to enhancement

comment:11 Changed 4 years ago by dmethvin

  • Keywords needsdocs added

comment:12 Changed 4 years ago by rwaldron

  • Blocking 5048 removed

(In #5048) This needs to be reverified with the new ajax module.

comment:5 Changed 4 years ago by rwaldron

  • Keywords getScript,ajax,needsdocs,ajaxrewrite added; getScript ajax needsdocs removed

comment:6 Changed 4 years ago by rwaldron

  • Keywords ajaxrewrite added; getScript,ajax,needsdocs,ajaxrewrite removed

comment:7 Changed 4 years ago by addyosmani

  • Keywords ajaxrewrite, needsdocs added; ajaxrewrite removed
  • Status changed from open to closed
  • Resolution set to wontfix

I've just gone through the API docs for .getScript() and also put together a a live test case here:  http://jsfiddle.net/64bDU/ using jQuery 1.5.2 demonstrating that the two parameters accepted by the success callback do in fact return values and *not* null/undefined as originally posted. In the case of the first parameter (data) you of course don't expect to get data returned, however the textStatus returns with a success message on completion.

In my view, we may need to adjust the docs to clarify the limitations of .getScript(), however as the first reply correctly points out, if you need more than it provides, remember it's just a shorthand method and you can always fall back on $.ajax() for something requiring additional callbacks for errors etc. Closing as an error callback would probably creep into the territory of stopping this being just a short convenience path to achieving $.ajax()-related functionality.

Version 0, edited 4 years ago by addyosmani (next)

comment:8 Changed 3 years ago by addyosmani

  • Keywords neededdocs added; needsdocs removed
Note: See TracTickets for help on using tickets.