PHP header() called via AJAX not working properly

后端 未结 4 1040
轮回少年
轮回少年 2021-01-25 06:29

I\'m new to web development.

Right now I\'m working on a login feature on a site. I used Javascript/AJAX to fetch the username and password and send it to a PHP file for

4条回答
  •  庸人自扰
    2021-01-25 07:24

    I don't think the redirect will work with AJAX. This is what will happen:

    1. AJAX request is sent to login.php
    2. login.php sends back a header with Location: profile.php
    3. The browser then redirects and fetches profile.php
    4. The results of profile.php is then passed to your XMLHttpRequest Object.

    A way to get the AJAX response to redirect your page is to do this:

    • The response from login.php returns a JSON response containing the status code (302 or 301) and the location to redirect to.

    • The response itself has a status code of 200 (successful).

    • The process the JSON response and check the status code for 302 or 301 and redirect to the location.

提交回复
热议问题