Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#10124 closed bug (invalid)

$.ajax doesn't work with JS as response on IE9

Reported by: andrey.turkin@… Owned by:
Priority: low Milestone: None
Component: ajax Version: 1.6.2
Keywords: Cc:
Blocked by: Blocking:

Description

Here is code fragment to illustrate the problem:

<a href="/js">click me</a>
<div id="results"></div>
<script>
  $(function() {
    $('a').click(function() {
      $.ajax({url: $('a').attr("href")});
      return false;
    });
  });
</script>

GET /js returns simple javascript like

$('#results').append("hi");

This works with Firefox and Chrome but doesn't work with IE9 (9.0.8112.16421 on Win7) - jQuery tries to read xhr.responseXML and IE throws an "Unspecified exception".

Change History (2)

comment:1 Changed 8 years ago by addyosmani

Component: unfiledajax
Priority: undecidedlow
Resolution: invalid
Status: newclosed

From the sounds of it, /js is outputting JavaScript which you wish to be loaded and executed when the user clicks on a link. In order to achieve this you need to pass a dataType parameter with a value of 'script' back to $.ajax (eg. dataType: "script"). Alternatively, use $.getScript() to achieve the same. http://api.jquery.com/jQuery.getScript/

comment:2 Changed 8 years ago by andrey.turkin@…

From the sounds of it, /js is outputting JavaScript which you wish to be loaded and executed when the user clicks on a link.

Yes.

In order to achieve this you need to pass a dataType parameter with a value of 'script' back to $.ajax (eg. dataType: "script").

Two issues here:

1) according to documentation, dataType will be determined from response content-type unless specified explicitly. In this example /js sends back "text/javascript" content type so explicit dataType shouldn't be needed

2) dataType: "script" doesn't work as well as getScript (which does the same) - browser sends smaller set of Accept types which includes "text/javascript", server responds just like with unspecified dataType, everything else is just the same.

Exception happens at https://github.com/jquery/jquery/blob/1.6.2/src/ajax/xhr.js#L145 which, if I understood correctly, happens before $.ajax callback is executed. So I think this is valid issue

Note: See TracTickets for help on using tickets.