0

你好逻辑思想家!!,我试图从一个用 PHP 编写的函数调用返回的数组中返回一个 JSON 对象。昨天它正在返回对象,但不知何故我设法打破了它。如果有人可以请帮助我将不胜感激。下面是我的 Javascript 和 PHP 中列出的代码......再次感谢可能提供的任何帮助。

--PHP--

<?php
class FileSel{

public $event;
public $posUsr;
public $posPass;
public $posAddr;
public $scUsr;
public $scPass;
public $scAddr;
public $cclUsr;
public $cclPass;
public $confName;

//Variable used to hold the connection
var $scp;
//Variable used to hold the final list of files to be displayed to the user to choose from
var $List = array();
//Variable used to hold the list of possible SC files
var $SCFiles = array();
//Variable used to hold the list of possible POS files
var $POSFiles = array();
//Variable used to hold the list of possible CCL files
var $CCLFiles = array();

function __construct(){
    $this->debug = new debug();
}

public function execute(){
    $this->debug->putLog("Made it to FileSel->execute()\n");
    return new Response(true, "Build List executed", json_encode($this->buildList()));
}

public function load_post_data(){
    $this->debug->putLog("Made it to FileSel->load_post_data()\n");
    $this->event = $_POST['event'];
    $this->posUsr = $_POST['pos_user'];
    $this->posPass = $_POST['pos_password'];
    $this->posAddr = $_POST['pos_address'];
    $this->scUsr = $_POST['sc_user'];
    $this->scAddr = $_POST['sc_address'];
    $this->cclUsr = $_POST['ccl_user'];
    $this->cclPass = $_POST['ccl_password'];
    $this->confName = $_POST['confName'];

    if( strlen($this->event) <= 0) return false;
    if( strlen($this->posAddr) <= 0) return false;
    if( strlen($this->posUsr) <= 0) return false;
    if( strlen($this->posPass) <= 0) return false;
    if( strlen($this->scAddr) <= 0) return false;
    if( strlen($this->scUsr) <= 0) return false;
    if( strlen($this->scPass) <= 0) return false;
    if( strlen($this->cclUsr) <= 0) return false;
    if( strlen($this->cclPass) <= 0) return false;
    if( strlen($this->confName) <= 0) return false;

    return true;
}

public function buildList(){
    $this->debug->putLog("Made it to FileSel->buildFileArray()\n");    
    //Build a list of files from the POS
    $this->listPOSFiles();
    //Build a list of files from the SC
    $this->listSCFiles();
    //Build a list of files from the CCL
    $this->listCCLFiles();
    $this->debug->putLog("Building final file list\n");
    //Variable used to keep track of index of final list
    $j=0;

    //Here we are adding POS files to the final list
    foreach($this->POSFiles as $file){
        $this->List[$j] = $file;
        $this->debug->putLog("Added POS File: $file to final file list\n");
        $j++;
    }

    //Here we are adding POS files to the final list
    foreach($this->SCFiles as $file){
        $this->List[$j] = $file;
        $this->debug->putLog("Added SC File: $file to final file list\n");
        $j++;
    }

    //Here we are adding POS files to the final list
    foreach($this->CCLFiles as $file){
        $this->List[$j] = $file;
        $this->debug->putLog("Added CCL File: $file to final file list\n");
        $j++;
    }

    return $this->List;
    }
    }

?>

--Javascript--

function displayDialog(){
    //console.log("Made it to displayDialog");
    var fileData = {
        "event" : "createFileSelectDialog",
        "pos_address" : file_pos_address,
        "confName" : file_confName,
        "pos_user" : file_pos_user,
        "pos_password": file_pos_password,
        "ccl_user" : file_ccl_user,
        "ccl_password": file_ccl_password,
        "sc_address" : file_sc_address,
        "sc_user" : file_sc_user,
        "sc_password": file_sc_password
    };

    $.ajax({
        url: './lib/app.php',
        data:  fileData,
        type: 'POST',
        dataType : 'json',
        success: function(data){
            console.log(data);
            alert("Data Loaded: " + data);
        },
        complete: function(data) {
            console.log(data);
            }
        });
    }

如果我可以提供任何其他有助于解决此问题的信息,请告诉我,我会发布。

4

1 回答 1

0

我不熟悉 Response 类;你在使用框架吗?如果是这样,我会查看源代码,看看调用它的构造函数时会发生什么。例如,它是否会自动打印第三个参数以输出,或者您是否必须在实例化对象上手动打印属性或方法,或者可能会返回输出文本?根据功能的不同,它可以简单到将“return new Responce(...)”替换为“echo new Response(...)”。

于 2012-12-14T20:57:36.843 回答