1

Hey guys I got a 302 error code when I want to redirect my page, I'm using header()

I searched over google if I could find anything but I found nothing that was relevant for me, so maybe you guys can find it ;)

I get in the network tab in element inspecting 2 responses 1 is of my posting function in jQuery and the other is of the header('Location: index.php'); and that gives a response within Reload page to pick up source code for: http://localhost/porfolio/index.php

I did the header('HTTP/1.1 200 OK'); cuz I saw it would force it to give it a 200 OK code. But when I did that it didn't even give the response of the header('Location: index.php');

I just wan't to redirect the person to index.php when he submitted and all fields are oke.

Response:

if (!empty($error))
{
    foreach ($error as $errors)
    {
        echo '<span class="glyphicon glyphicon-warning-sign" style="padding-right:5px; color:red;"></span><a class="errors" style="color:red;">' . $errors . '</a><br>';
    }
}
else
{
    header('Location: index.php');
    header('HTTP/1.1 200 OK');
}

Request

posting.done(function(data){
    var errors = $('<div>' + data + '</div>').find('.errors');
    if (errors.length > 0)
    {
        $("#gb-errors").slideDown().empty().append(data);
    }
    else
    {
        $("#gb-errors").slideUp().empty();
    }
});
  • 1
    [HTTP 302](https://en.wikipedia.org/wiki/HTTP_302) is a redirection status code, not an error code. Error codes are 4xx and 5xx codes. So issueing a 200 status code after a location header is senseless, see the dialog example in wikipedia. – syck Oct 08 '15 at 07:42
  • Oke thx for the explenation! I edited the title of my post ;) –  Oct 08 '15 at 07:43
  • 1
    More clearly expressed, `header('Location: index.php'); header('HTTP/1.1 200 OK');` is nonsense. – syck Oct 08 '15 at 07:46
  • @syck oke thx I won't use it anymore ;) –  Oct 08 '15 at 08:17
  • How long did your search last? 30 seconds? Did you even read the wikipedia articles on HTTP headers? – syck Oct 08 '15 at 08:54
  • @syck Sorry, but if you want to think that, I'm not like that. Its just I not always understand it! And this was very clever for me. –  Oct 08 '15 at 09:03

1 Answers1

0

You cannot redirect the page within an ajax request. You can use JQuery redirects instead.

Vivek Srivastava
  • 569
  • 4
  • 13
  • Thx this worked, only I can't pleace the code `windows.location.href.. ` in my `PHP else {}`. It only works when I put it in my `jQuery else {}` –  Oct 08 '15 at 07:40
  • 1
    Yes it won't work in PHP as you have request for response from server through Ajax but if you redirect on PHP side then headers change and thus 302 error occurs. – Vivek Srivastava Oct 08 '15 at 07:45
  • 302 is not an error code, as stated above. The location header is prefixed by PHP with a `302 Found` redirection header, because this is the appropriate header for the redirection. – syck Oct 08 '15 at 08:53