简体   繁体   English

Jaxon 不会在响应中返回任何内容

[英]Jaxon doesn´t return anything on response

Please, can anyone help?请问,有人可以帮忙吗?

I´m starting to use jaxon 3, planning to migrate from xajax.我开始使用 jaxon 3,计划从 xajax 迁移。 On a php7.3 plataform, I made a simple code, just to load jaxon and run a single alert script, but its returning nothing on browser.在一个 php7.3 平台上,我编写了一个简单的代码,只是为了加载 jaxon 并运行一个警报脚本,但它在浏览器上没有返回任何内容。 There are no errors on server log and no info on browser, no even on the chrome debug console.服务器日志上没有错误,浏览器上也没有信息,甚至在 chrome 调试控制台上也没有。

Here is the simple code:这是简单的代码:

<?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: I didn´t use the jaxon word on tag cause the editor doesn´t let me do it PS:我没有在标签上使用 jaxon 词,因为编辑不让我这样做

I would tell you to delete the echo at the end, and try to code some functions even for testing, so you can better understand v3.我会告诉你最后删除回声,并尝试编写一些函数甚至进行测试,这样你就可以更好地理解 v3。

Here, check this code, it might help you, this code is tested and will work.在这里,检查此代码,它可能会对您有所帮助,此代码已经过测试并且可以工作。

<?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