Side navigation
#11006 closed bug (invalid)
Opened December 13, 2011 09:12AM UTC
Closed December 13, 2011 12:55PM UTC
Last modified March 14, 2012 04:58PM UTC
$.ajax() dont trigger success-Method when PHP-Header is set
Reported by: | timohuber@me.com | Owned by: | timohuber@me.com |
---|---|---|---|
Priority: | undecided | Milestone: | None |
Component: | unfiled | Version: | 1.7.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Hi,
when I request a PHP site with header "application/javascript", the "success" method get not triggered (only the "error" method).
The server return code 200
HTML Code:
<!DOCTYPE html> <html lang="de"> <head> <title>test</title> <!-- meta --> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <!-- javascript --> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> function withHeader() { test({header: 1}); } function withoutHeader() { test({header: 0}); } function test(data) { $.ajax({ url: 'test.php', type: 'GET', data: data, error: function(result) { console.debug('error'); console.debug('readyState: ' + result.readyState); console.debug('status: ' + result.status); $('#output').html('error'); }, success: function(result) { console.debug('success'); $('#output').html('success'); } }); } </script> </head> <body> <button type="button" onclick="withHeader();">test withHeader</button> <button type="button" onclick="withoutHeader();">test withoutHeader</button> <p id="output"></p> </body> </html>
PHP Code (needs server):
<?php if (isset($_GET['header']) && $_GET['header']) { header('Content-Type: application/javascript'); } echo '{varname: varvalue}';
greets
Timo
Attachments (0)
Change History (5)
Changed December 13, 2011 10:44AM UTC by comment:1
owner: | → timohuber@me.com |
---|---|
status: | new → pending |
Changed December 13, 2011 12:55PM UTC by comment:2
resolution: | → invalid |
---|---|
status: | pending → closed |
What you're echoing is not valid javascript, it's not even valid json.
Please use our forum or the #jquery channel on irc.freenode.net for this kind of support questions.
Changed December 19, 2011 09:45PM UTC by comment:3
This issue should not have been closed. His code is an example only. I am experiencing this same issue when attempting to use $.ajax to retrieve JSON returned by PHP. The success handler fires fine when using jQuery 1.6.3, but an upgrade to 1.7.1 breaks it.
Changed March 14, 2012 04:43PM UTC by comment:4
That's real bug, in earlier versions all work fine. JSON response absolute valid.
Changed March 14, 2012 04:58PM UTC by comment:5
Replying to [comment:4 busy-v@…]:
That's real bug, in earlier versions all work fine. JSON response absolute valid.
OOhhh, sorry. Trouble was in incorrect ContentType header. For json output header must be application/json but not text/htm or text/javascript.
Thanks for taking the time to contribute to the jQuery project! Please provide a complete reduced test case on jsFiddle to help us assess your ticket.
Additionally, be sure to test against the jQuery Edge version to ensure the issue still exists. To get you started, use this boilerplate: http://jsfiddle.net/FrKyN/ Open the link and click to "Fork" (in the top menu) to get started.