哪位帮我看看我这个代码出什么问题了,被纠结一天了
这是前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="无刷新评论.aspx.cs" Inherits="传智播客.ajax.无刷新评论" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
#TextArea1 {
height: 60px;
width: 184px;
}
</style>
<script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var comment = $("#TextArea1").val();
$("#Button1").click(function () {
$.post("noFreshComment.ashx", { "comment": comment }, function (data, status) {
if (status == "success") {
if (data == "插入成功") {
alert("评论成功");
}
}
})
});
})
</script>
</head>
<body>
<form runat="server">
<div>
<textarea id="TextArea1"></textarea>
<br />
<input type="button" value="评论" id="Button1"/>
</div>
</form>
</body>
</html>
这是后台ashx代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using 传智播客.DAL.DataSetCommentTableAdapters;
using System.Windows.Forms;
namespace 传智播客.ajax
{
/// <summary>
/// 无刷新评论1 的摘要说明
/// </summary>
public class 无刷新评论1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string comment=context.Request["comment"];
MessageBox.Show(comment);
if (comment!= "")
{
T_CommentTableAdapter adapter = new T_CommentTableAdapter();
adapter.InsertNewComment(comment);
context.Response.Write("插入成功");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
为什么调试的时候发现comment=""呢?
$("#TextArea1").val(); 有没有取到值 取到了的话 试试这样写
$.ajax({
type: "Post",
url: "noFreshComment.ashx",
data: "comment=comment,
success: function (msg) {
if (msg == "success") {
}
}
});
noFreshComment.ashx 页面 这样拿值 :
string comment= context.Request.Params["comment"].ToString();