自动完成在本地主机上工作,但不在服务器上工作

Autocomplete working on localhost, but not on server

我有一个带有自动完成功能的搜索公式,它在本地主机上工作得很好,但是一旦我把它放在远程服务器上它就停止工作了。

希望你能帮助我。 这是一些代码:

index.php:

<!DOCTYPE html>
<html lang="de">
 <head>
    <?
    header("Content-Type: text/html; charset=iso-8859-1"); 
    
    ?>

    <link rel="stylesheet" href="css/jquery-ui-1.10.3.custom.min.css" />
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    
    <link rel="stylesheet" href="css/style.css" />
    

    <script src="js/jquery-1.10.2.min.js"></script> 
    <script src="js/jquery-ui-1.10.3.custom.min.js"></script>
    <script src="js/bootstrap.min.js"></script> 
</head>
<body>  

    <div id="wrap">
        <h1 class="text-center">Suche</h1>
        <div class="row">
            <div class="col-xs-6 col-sm-4 col-md-4 col-xs-offset-6 col-sm-offset-4 col-md-offset-4">
                <form method='POST' action=''>
                <input type='text' name='food' id="country_name" class="form-control txt-auto"/>
                <input type='submit' value='search'>
                </form>

            </div>

        </div>
        
    </div>
    

    
    <script src="js/auto.js"></script>
</body>
</html>

ajax.php

<?php


header('Content-Type: text/html; charset=UTF-8');

require_once 'config.php';

if($_GET['type'] == 'country'){
    $result = mysql_query("SELECT *
        FROM table
        WHERE name LIKE '%".strtoupper($_GET['name_startsWith'])."%'
        LIMIT 8");  
    $data = array();
    while ($row = mysql_fetch_array($result)) {
        array_push($data, $row['name']);    
    }   
    echo json_encode($data);
}

?>

auto.js

   $('#country_name').autocomplete({
                    source: function( request, response ) {
                        $.ajax({
                            url : 'ajax.php',
                            dataType: "json",
                            data: {
                               name_startsWith: request.term,
                               type: 'country'
                            },
                             success: function( data ) {
                                 response( $.map( data, function( item ) {
                                    return {
                                        label: item,
                                        value: item
                                    }
                                }));
                            }
                        });
                    },
                    autoFocus: true,
                    minLength: 0        
                  });
              
        

您在 HTML 中遗漏了 php 并且如果您的服务器没有设置短开放标签 - 可能会导致问题,如评论中所示 - 不应该是 header 声明之前的任何 html 内容。

应该是:

   <?php
    header("Content-Type: text/html; charset=iso-8859-1"); 
    ?>