如何在AJAX中编写PHP

我试图让我的会话在ajax ..因为我已经编写了这样的代码

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(); setTimeout(function() { ); BTLJ(".kcregbox").show();},7000); // BTLJ("#btl-success").hide(); } }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(textStatus + ': Ajax request failed'); } }); 

但如果我这样写,ajax就无法工作了……请帮助我在ajax中开会。 提前致谢。

您在Joomla中的AJAX查询应格式如下。

  jQuery.post('index.php',{ 'option' : 'com_componentname', 'controller' : 'controllername', 'task' : 'task_name', 'format' : 'raw', 'data' : data }).success(function(result) { //if the request is success }).error(function() { //if the request is fails }); 

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

 $.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数组将客户端所需的所有数据