簡體   English   中英

Jaxon 不會在響應中返回任何內容

[英]Jaxon doesn´t return anything on response

請問,有人可以幫忙嗎?

我開始使用 jaxon 3,計划從 xajax 遷移。 在一個 php7.3 平台上,我編寫了一個簡單的代碼,只是為了加載 jaxon 並運行一個警報腳本,但它在瀏覽器上沒有返回任何內容。 服務器日志上沒有錯誤,瀏覽器上也沒有信息,甚至在 chrome 調試控制台上也沒有。

這是簡單的代碼:

<?php


require_once( 'Jaxon/vendor/autoload.php' );
use Jaxon\Jaxon;
use Jaxon\Response\Response;

$ajax = jaxon();
$ajax->setOption('core.debug.on', false);

$ajax->setOption('core.prefix.function', 'jaxon_');

$ajax->setOption('core.request.uri', 'ajax.php');
$objResponse = new Response();



$objResponse->alert('test');
return $objResponse;
echo 'tests';
?>

PS:我沒有在標簽上使用 jaxon 詞,因為編輯不讓我這樣做

我會告訴你最后刪除回聲,並嘗試編寫一些函數甚至進行測試,這樣你就可以更好地理解 v3。

在這里,檢查此代碼,它可能會對您有所幫助,此代碼已經過測試並且可以工作。

<?php
require_once ($_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php');

use Jaxon\Jaxon;
use Jaxon\Response\Response;

$jaxon = jaxon();

$jaxon->setOption('js.app.minify', TRUE);
$jaxon->setOption('js.lib.uri', '/vendor/jaxon-php/jaxon-js/dist');


/** ################################# */
/** using jaxon with just functions then you have to register each function 
 * 
 *  call these functions like this:
 *  jaxon_demo1();
 *  jaxon_demo2();
 * 
 *  <button type="button" onclick="jaxon_demo1()"> call fn 1 </button>
 *  <button type="button" onclick="jaxon_demo2()"> call fn 2 </button>
 *  
*/
$jaxon->register(Jaxon::USER_FUNCTION, 'demo1'); 
$jaxon->register(Jaxon::USER_FUNCTION, 'demo2'); 
$jaxon->register(Jaxon::CALLABLE_FUNCTION, 'functionThatReturnsUsing_setReturnValue', ['mode' => "'synchronous'"]); 

function demo1(){
    $jaxonResponse = new Response();
    $jaxonResponse->alert('Hello there, be sure to open browser console to see the console.log');
    $jaxonResponse->script("console.log('Hello there')");
    
    return $jaxonResponse;
}

function demo2(){
    $jaxonResponse = new Response();

    // <div id="theDiv"> this will be replaced </div>
    $jaxonResponse->assign("theDiv","innerHTML",'Some content that can even be a template if you implement smarty or similar');
    
    return $jaxonResponse;
}

/**
 *  check out https://github.com/jaxon-php/jaxon-js/issues/15
 *  this should work, take a look at the link provided
 *  also use the browser console to look the response data being returned
 */
function functionThatReturnsUsing_setReturnValue(){
    $jaxonResponse = new Response();
    // lets suppose you have a script like this
    // <script>
    //    function demo3(){
    //        var myData = jaxon_functionThatReturnsUsing_setReturnValue();
    //        console.log(myData);
    //    }   
    // </script>
    $someData = array(
    'isValidated' => TRUE,
    'str_data' => 'some data here',
    'int_data' => 123,
    'flt_data' => 1.23,
    'row_data' => array(
        'isValidated' => TRUE,
        'str_data' => 'some data here',
        'int_data' => 123,
        'flt_data' => 1.23
        )
    );
    $jaxonResponse->setReturnValue($someData);
    $jaxonResponse->getOutput();
    return $jaxonResponse;
}    
    
if($jaxon->canProcessRequest()){
    $jaxon->processRequest();
}

/**
 * if you are using composer, then your composer.json should have this at least:
 * 
 * {
 *     "require": {
 *         "jaxon-php/jaxon-core": "^3.2",
 *         "jaxon-php/jaxon-js": "^3.2"
 *     }
 * }
 * 
 */

?>
<!DOCTYPE html>
<html lang="es">
<head>
    <title>Demo jaxon</title>
    <?php echo $jaxon->getCss(); ?>
</head>
<body>
    <ul>
        <li><a href="javascript:void(0)" onclick="jaxon_demo1()">demo fn 1</a></li>
        <li><a href="javascript:void(0)" onclick="jaxon_demo2()">demo fn 2</a></li>
        <li><a href="javascript:void(0)" onclick="demo3()">demo fn 3</a></li>
    </ul>
    <div id="theDiv">this will be replaced</diV>
    
    <script>
        function demo3(){
            var myData = jaxon_functionThatReturnsUsing_setReturnValue();
            console.log(myData);
        }   
    </script>
    <?php
        echo $jaxon->getJs();
        echo $jaxon->getScript();
    ?>
</body>
</html>

暫無
暫無

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

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