json传值问题

悠悠虾 2011-06-10 08:07:08
帮忙看一下代码:
<script type="text/javascript">
$(document).ready(function () {
$("#btnComment").click(function () {
var txtComment = $("#txtComment").val();
$.ajax({
type: "post",
url: "CsdnAddComment.aspx/AddComment",
data: " { 'comment':txtComment}",
contentType: "application/json; charset=utf-8",
datatype: "json",
success: function (message) {
alert(message);
}
});
});
});
</script>

我加粗的那句话,假如按照上面那么写的话txtComment这个值没法传到后台,但是假如我改成'txtComment'(加上引号)这样的话值就可以传过去,但这传的值肯定是txtComment,而不是我想要的txtComment变量的值。应该怎么写呢?
...全文
128 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
inetfuture 2011-06-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yuxh81 的回复:]
典型的字符串拼接:

" { 'comment':'"+txtComment+"'}"
[/Quote]
拼接字符串是这种写法:data: 'comment=' + txtComment + '&key=value&key=vlaue……',
none99999 2011-06-10
  • 打赏
  • 举报
回复
你那个写的有问题:contentType: "application/json; charset=utf-8",

var txtComment = $("#txtComment").val();
alert(txtComment);
$.ajax({
type: "post",
url: "/Home/getdata",
data: 'comment=' + txtComment,
datatype: "json",
success: function (message) {
alert(message);
}
});
hch126163 2011-06-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 inetfuture 的回复:]

data: { comment:txtComment},或者:
data: { 'comment':txtComment},
[/Quote]

+++
zell419 2011-06-10
  • 打赏
  • 举报
回复
{comment : txtComment} ,
yuxh81 2011-06-10
  • 打赏
  • 举报
回复
典型的字符串拼接:

" { 'comment':'"+txtComment+"'}"
inetfuture 2011-06-10
  • 打赏
  • 举报
回复
data: { comment:txtComment},或者:
data: { 'comment':txtComment},

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧