$.ajax数据无法保存,数据没有得到保存使用JQuery.ajax

阎经武
2023-12-01

我想保存按钮点击文本框中的数据。我正在使用JQuery AJAX来执行此任务,如下所示。请注意,我在主题功能中制作了这个标签。数据没有得到保存使用JQuery.ajax

function theme_user_post_block($vars)

{

$themeUserCommentInput ='';

$themeUserCommentInput .= '';

$themeUserCommentInput .= '

class="btnPostComment" id="btn_1" />'

return $themeUserCommentInput;

}

这能够显示我的文本框和按钮里面的页面。现在,这里是我的JS代码: -

(function($)

{

Drupal.behaviors.PostComment= {

attach: function (context, settings) {

$('.btnPostComment', context).click(function (event) {

var post = "&newcomment=Comment1&logid=log1";

jQuery.ajax({

url: 'postcomment',

type: 'POST',

dataType: 'json',

data: post,

success: function (data) { alert(data); },

error: function(jqXHR, textStatus, errorThrown){alert(textStatus +

errorThrown);}

});

});

}

}

})(jQuery);

接下来,我创建一个URL名称的菜单页如下: -

function postcomment_menu(){

$items=array();

$items['postcomment']=array(

'title'=>t(''),

'type'=> MENU_CALLBACK,

'page callback' => 'user_comment_post',

'access arguments' => array('access content'),

);

return $items;

}

function user_comment_post(){

global $user;

$cid = db_insert('user_comment')

->fields(array(

'comment_user_id' => $user->uid,

'reference_id' => $_POST['logid'],

'comment_desc'=>$_POST['newcomment'],

'createdon'=>REQUEST_TIME,

))

->execute();

if($cid!=0)

{

//GetUserComments($i);

drupal_json_output("success");

}

}

所以我这样做所需的jQuery + Ajax的所有东西提交功能。当我按下“发表评论”按钮时,它给我错误警报说“errorundefined”。该警报显示为jQuery.AJAX函数内部的错误结果。此外,自定义菜单回调也没有被调用。

+0

您的示例代码看起来不正确。即使从复制粘贴,请确保您显示的代码是有效的格式,否则我们不知道您是否有这个问题是因为它。在这里''textarea id =“txt_1'”rows =“1”cols =“50”>';''textarea'的'id'属性'txt_1'中有一个额外的''' ',搞乱了格式。 –

2013-05-11 15:54:48

+0

对不起,我的代码错误。实际上,为简单起见,id中的动态值被静态值替换。我编辑了编码部分。 –

2013-05-11 18:25:29

 类似资料: