【问题标题】:Jaxon doesn´t return anything on responseJaxon 不会在响应中返回任何内容
【发布时间】:2020-04-08 14:58:06
【问题描述】:

请问,有人可以帮忙吗?

我开始使用 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 词,因为编辑器不让我这样做

【问题讨论】:

    标签: ajax php-7.3 xajax


    【解决方案1】:

    我会告诉你删除最后的回声,并尝试编写一些函数甚至用于测试,这样你可以更好地理解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>
    

    【讨论】:

      猜你喜欢
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多