如何在 AJAX 中编写 PHP

2022-01-06 00:00:00 jquery php ajax joomla

我正在尝试在 ajax 中获取我的会话 .. 为此我编写了这样的代码

I am trying to get my session in ajax.. for that i had written my code like this

BTLJ.ajax({
           type: "POST",
           url: btlOpt.BT_AJAX,
           data: datasubmit,
           success: function(html){
                //if html contain "Registration failed" is register fail
              BTLJ("#btl-register-in-process").hide();  
              if(html.indexOf('$error$')!= -1){
                  ...
                  ...
                  }
               }else{                  
                   BTLJ(".btl-formregistration").children("div").hide();
                   BTLJ("#btl-success").html(html); 
                   BTLJ("#btl-success").show(); 
                   alert(<?php session_start(); print_r($_SESSION); ?>);
                   setTimeout(function() { ); BTLJ(".kcregbox").show();},7000);
                  // BTLJ("#btl-success").hide();
               }
           },
           error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus + ': Ajax request failed');
           }
        });

但是如果我这样写,ajax 将不起作用.. 请帮助我在 ajax 中获得我的会话.提前致谢.

but ajax is not working if i write like that.. please help me in getting my session in ajax. thanks in advance.

推荐答案

我在 joomla 中为 ajax 使用这种格式

I'm using this format for ajax in joomla

$.ajax({
        type: 'GET',
        url: 'index.php', 
        data: {option: 'com_componenetname', task: 'taskname.youroperation', format: 'json', tmpl: 'raw'},
    dataType: 'json',
        async: true, // can be false also
        error: function(xhr, status, error) {
                console.log("AJAX ERROR in taskToggleSuceess: ")
                var err = eval("(" + xhr.responseText + ")");
                console.log(err.Message);
                },
        success: function(response){

                // on success do something
                // use response.valuname for server's data
                        }
                ,
        complete: function() {
            // stop waiting if necessary 
                 }                     
          });

在您的组件/控制器中,您应该有一个文件 yourcontroller.json.php 它将处理您的调用并返回编码的 json 数组将您需要在客户端中的所有数据

In your component/controllers you should have a file yourcontroller.json.php which will process your call and return encoded json array will all the data you need in the client

相关文章