从php ajax响应JSON对象创建jquery数组

时间:2018-05-29 12:01:45

标签: php jquery arrays json store

我有一个 en.php 文件,里面有一个数组,如下所示:

$lang = array(
    // MENÚ LATERAL
    "bienvenido" => "Welcome",
    "panel_administracion" => "Administration panel",
    "administracion" => "Administration",
    "gestion_usuarios" => "Users Management",
    "roles_permisos" => "Roles and Permissions",
    "perfiles" => "Profiles",
    "apariencia" => "Appearance",
    "configuracion" => "Settings",
    "gestion_scripts" => "Script Management",
    "gestion_escenarios" => "Scenario Management",
    "planificador" => "Planner",
    "monitorizacion" => "Monitoring",
    "resultados_ejecuciones" => "Executions Results",
    "dashboard" => "Dashboard",
    // USUARIOS
    "usuario" => "User",
    "nombre_pagina" => "Users",
    "nuevo_usuario" => "New User",
    "nombre_usuario" => "User Name",
    "perfil" => "Profile",
    "rol" => "Role",
    "idioma_sitio" => "Language",
    "password" => "Password",
    "repetir_password" => "Repeat Password",
    "guardar" => "Save",
    "administracion_usuarios" => "User administration",
    "actualizar_usuario" => "Edit user",
    "acciones" => "Actions"
);

我想将这个数组存储在一个js数组中,在.js文件中,以便随时访问我需要的数据,这段代码就是这样做的。但我收到此警告: [Deprecation]主线程上的同步XMLHttpRequest由于其有害的而被弃用...

var traducciones = function () {
    var tmp = null;
    var action = "getTraducciones";
    $.ajax({
        'async': false,
        'type': "POST",
        'global': false,
        'dataType': 'html',
        'url': '../egea/lib/egeaAjax/fAjaxUsuarios.php',
        'data': { 'action': action },
        'success': function (data) {
            var datos = JSON.parse(data);
            tmp = datos;
        }
    });
    return tmp;
}();

PHP函数:

function getTraducciones(){
    session_start();
    $traducciones = '';
    $u = $_SESSION['usuario'];
    if (isset($u) === true){
        $lang = getLanguage($u);
        if(isset($lang) === true){
            require "../../lang/".$lang.".php";
        }else{
            require "../../lang/es.php";
        }
    }
    echo json_encode($lang);
}

注意: async:false已弃用。 如果没有ajax async:false ,我怎么能这样做呢? 感谢。

1 个答案:

答案 0 :(得分:0)

返回承诺而不是价值本身:

var traducciones = function () {
    return new Promise((resolve, reject) => {
        var action = "getTraducciones";
        $.ajax({
            'async': true,
            'type': "POST",
            'global': false,
            'dataType': 'html',
            'url': '../egea/lib/egeaAjax/fAjaxUsuarios.php',
            'data': { 'action': action },
            'success': function (data) {
                var datos = JSON.parse(data);
                resolve(datos);
            },
            'error': reject
        });
    });
}();

然后使用它:

traducciones.then(function(datos) {
    // the code that uses the data
}, function() {
    // code called if data loading failed
});

顺便说一句,如果你真的需要以同步的方式加载数据,你可以考虑将它加载为JS代码而不是JSON - 即替换:

echo json_encode($lang);

在您的PHP代码中:

Header('Content-type: text/javascript; charset=utf-8');
    // or whatever other charset you are using ^^^^^
echo "var traducciones = " . json_encode($lang) . ";\n";

然后将其作为普通脚本加载:

<script src="../egea/lib/egeaAjax/fAjaxUsuarios.php"></script>