简体   繁体   中英

Calling php script with Ajax results in internal server error, refresh works?

I'm working on a dashboard that displays various data about remote servers. It switches between servers with buttons on a sidebar. For example, this is the html that calls the script:

onclick="tab('localhost')"

This is the Ajax bit:

function tab(host){
        $.ajax({
                url: 'remote.php',
                data: { tab: host },
                success: function(result){
                        $('.container-body').html(result);
                },
                error: function(xhr, status, error) {
                    $('.container-body').html("<h1>" + error + "</h1>when opening " + host);
                },
                complete: function(){
                        $(init_ui);
                        $(update);
                        $('.tab').removeClass('active');
                        $('#dash').removeClass('active');
                        $('#'+host).addClass('active');
                }
        });
}

This should set $_SESSION["tab"] to "localhost" and then display data for that server. However, it returns "Internal Server Error" . If however I refresh the page, it loads normally with the session set to "localhost".

If I remove the GET parameter from the Ajax call and the isset() function from remote.php, the Ajax function returns success with no session, hinting that this is where the problem originates.

Here is the php configuration of remote.php

<?php 
error_reporting(E_ALL ^ E_NOTICE);
session_start(); 
require_once 'config.php';
if(isset($_GET["tab"])) $_SESSION["tab"] = $_GET["tab"]; 
?>

Nothing special, right? So what the hell is going on here?!

EDIT:

For some reason Ajax is handling sessions incorrectly, and the incorrect session results in invalid PHP code.

NEW QUESTION:

What is special about sessions in Ajax?

You forgot the parentheses in the call to require:

require_once 'config.php';

should be:

require_once('config.php');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM