日期:2014-05-17  浏览次数:20430 次

ajax入门的代码呃,怎么这个我找了一上午都不知道错哪了,点按钮后没反应啊
<head runat="server">
  <title>无标题页</title>
  <script src="js/jquery-1.4.2.js" type="text/javascript"></script>
  <script type="text/javascript">
  $(function(){
  $("#btnPost").click(function(){
  $.post("PostComment.ashx",{"msg":$("#TextArea1").val()},function(data,status){
  if(status=="success"){
  if(data=="ok"){
  alert("评论发表成功");
  }
  else{
  alert("评论发表内容有问题!");
  }
  }
  else{
  alert("发表评论失败,请重试!");
  }
  });
  });
  });
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <textarea id="TextArea1" name="S1" rows="5" cols="30"></textarea><br />
  <input id="bntPost" type="button" value="提交" /></div>
  </form>
</body>
</html>

以下是同文件夹下的PostComment.ashx,单独测试是能返回"ok",能插入数据到数据库的

public class PostComment : IHttpHandler
  {

  public void ProcessRequest(HttpContext context)
  {
  context.Response.ContentType = "text/plain";
  string msg=context.Request["msg"];
  new T_PostsTableAdapter().Insert(context.Request.UserHostAddress, msg,DateTime.Now);
  context.Response.Write("ok");
  }

  public bool IsReusable
  {
  get
  {
  return false;
  }
  }
  }

------解决方案--------------------
具体请以下文章,代码基本差不多。

http://hi.baidu.com/callmequiet/blog/item/1f50f355d847521f86947337.html
------解决方案--------------------
jQuery.get('messageAjax.aspx',
{ txtname: jQuery("#txtname").val(), txtemail: jQuery("#txtemail").val(), txttel: jQuery("#txttel").val(), textarea: jQuery("#textarea").val() },
function (responseText, textStatus, XMLHttpRequest) {
alert(responseText);
});

我平时是这样写的,参考一下
------解决方案--------------------
探讨
尝试了,但是还是没有反映!

------解决方案--------------------
在$.post之前加个alert看看是ajax没有触发,还是click没有触发

然后用alert弹出data、status,先看看是不是返回的值有问题而判断错了

这样基本就能把问题找到了,然后再解决