簡體   English   中英

無法將數組從php函數返回到ajax請求

[英]Can't return array from php function to ajax request

我有一個javascript文件,其中我正在對foo.php執行ajax請求以獲取mysql結果數組。 這是我的javascript文件。

//foo.js
$(document).ready(function(){
    $.ajax({
        url:"foo.php",
        type:"POST",
        data:{
            action:"test",
            remail:"foo"
        },
        success:function(data){
            var messages = data;
            console.log("Ext req");
            try{
                console.log(JSON.parse(data));
            }catch(e){
                console.log(data);
            }
        },
        failure:function(){

        }
    })
});

為了從php接收我的結果數組,我執行以下操作:

//foo.php
<?php 
    if(isset($_POST)){
        $action = $_POST['action'];
        if(empty($action)){
            return;
        }
        switch($action){
            case "test":
                $query = "select * from message where seen";
                $ar = [];
                $res = mysqli_query($con,$query);
                while($row = mysqli_fetch_array($res)){
                    $ar[] = $row;

                }
               echo json_encode($ar);
            break;
        }
    }            
?>

這會將一個對象數組返回給我的ajax請求,然后根據我的需要處理。 但是,如果我嘗試將switch語句中的php代碼移動到函數中並返回函數的編碼結果,我只得到一個空數組作為響應。 以下是我嘗試這樣做的方法:

<?php 
    function test(){
        $query = "select * from message where seen";
        $ar = [];
        $res = mysqli_query($con,$query);
        while($row = mysqli_fetch_array($res)){
            $ar[] = $row;
        }
        return $ar;
    }
    if(isset($_POST)){
        $action = $_POST['action'];
        if(empty($action)){
            return;
        }
        switch($action){
            case "test":
                $result = test();
               echo json_encode($result);
            break;
        }
    }            
?>

任何想法為什么會這樣?

UPDATE

$con是來自我include另一個文件的變量

當您將查詢邏輯移動到函數$con ,MySQL的連接對象不可用。 使用GLOBAL $con; 在你的功能里面。

閱讀本文以了解可變范圍

方法1使用GLOBAL關鍵字

function test(){
    GLOBAL $con;

    $query = "select * from message where seen";
    $ar = [];
    $res = mysqli_query($con,$query);
    while($row = mysqli_fetch_array($res)){
        $ar[] = $row;
    }
    return $ar;
}

方法2將參數傳遞給函數

function test($con){
    $query = "select * from message where seen";
    $ar = [];
    $res = mysqli_query($con,$query);
    while($row = mysqli_fetch_array($res)){
        $ar[] = $row;
    }
    return $ar;
}

Call it like this:

test($con);

如果不仔細使用全局變量可能會使問題更難以找到其他解決方案建議。 $con作為參數傳遞給您的函數:

function test($con){
        $query = "select * from message where seen";
        $ar = [];
        $res = mysqli_query($con,$query);
        while($row = mysqli_fetch_array($res)){
            $ar[] = $row;
        }
        return $ar;
    }

讓我們談談你遇到的問題:

  1. 您將failure函數傳遞給$.ajax 你肯定想用error而不是failure 看到這里

  2. 您已初始化messages變量,但未success 擺脫它。

  3. 你檢查isset($_POST) ,但這將永遠是true 您想要檢查isset($_POST["action"]) ,或者您想檢查它是否是POST請求

  4. function內部沒有$con 您需要在function內部初始化它或將其傳遞給它並將其用作參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM