Ajax调用独立工作,但在wordpress中失败

时间:2017-03-19 23:32:26

标签: php jquery ajax wordpress

这段代码独立工作,但当它被放入wordpress时,它会产生一系列与ajax调用相关的错误。它遇到的第一个问题是它无法从远程源读取jquery,所以我下载了文件并将其读取到服务器的内部,但这些错误仍然存​​在,我不知道如何纠正这个。请注意,在wordpress中,我使用一个插件,允许我使用[php]和[/ php]在wordpress中使用php。

   <?php
$page_to_load = $_POST[view];
switch($page_to_load) {
    case '':
        echo "<script src=\"../scripts/jquery-3.2.0.min.js\"></script>";
        echo "<script type=\"application/javascript\" src=\"../scripts/raid.js\"></script>";
        echo "<font size=\"+3\" color=\"#FFFFFF\">Who should I host?<br>Please wait while channel is selected<br></font>";
        echo "<font size=\"+2\">";
        echo "<br><br>";
        echo "<img src=\"../_images/ajax_loader_blue_350.gif\">";
        $servername = "localhost";
        $username = "";
        $password = "";
        $dbname = "";
        $chanarray[] = null;

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        $sql = "SELECT `TwitchNames` FROM TK_Members WHERE Validated='1' AND RaidMe='1'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
             // output data of each row
             while($row = $result->fetch_assoc()) {
                  array_push($chanarray, $row['TwitchNames']);
            }
        } else {
           echo "0 results";
        }
        array_splice($chanarray, 0, 1);
        $conn->close();
        echo "<script type=\"application/javascript\">";
        echo "var channels = ". json_encode($chanarray); 
        echo "</script>";
        echo "</font>";
        echo "<form id=\"form\" method=\"post\">";
        echo "<input type=\"hidden\" name=\"view\" value=\"page2\">";
        echo "</form>";
    break;

    case 'page2':
        echo "<font size=\"+3\" color=\"#FFFFFF\">Who should I host?<br>";
        echo "Your channel to host is:<br></font>";
        echo "<font size=\"+2\">";
        echo "<br><br>";
        $chans[] = null;
        $test = $_POST['outArray'];
        foreach ($test as $chan) {
            $temparray = array(rand(),$chan);
            array_push($chans, $temparray);
        }
        array_splice($chans,0,1);
        sort($chans);
        echo "<a href=\"https://twitch.tv/".$chans[0][1]."\" target=\"_blank\">".$chans[0][1]."</a>";
        echo "<br><br><br>";
        echo "<a href=\"whoslive.htm\" target=\"_parent\">See All Live Channels</a>";
        echo "</font>";
    break;
}
?>

以下是错误:

ajax errors

1 个答案:

答案 0 :(得分:0)

$(document).ready(function() {更改为jQuery(function($){,因为WordPress在无冲突模式下运行jQuery。

来源:https://wordpress.org/support/topic/simple-jquery-not-working/