Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#11006 closed bug (invalid)

$.ajax() dont trigger success-Method when PHP-Header is set

Reported by: timohuber@… Owned by: timohuber@…
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

Change History (5)

comment:1 Changed 8 years ago by sindresorhus

Owner: set to timohuber@…
Status: newpending

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.

comment:2 Changed 8 years ago by jaubourg

Resolution: invalid
Status: pendingclosed

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.

comment:3 Changed 8 years ago by anonymous

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.

comment:4 Changed 8 years ago by busy-v@…

That's real bug, in earlier versions all work fine. JSON response absolute valid.

comment:5 in reply to:  4 Changed 8 years ago by busy-v@…

Replying to 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.

Note: See TracTickets for help on using tickets.