由於公司接下來的項目 會不斷用到ajax!
因此好好研究了下。
基本操作 挺簡單的!
HTML:
代碼
<form id="form1" runat="server">
<div>
<input type="text" id="commentTitle" /><br />
<textarea id ="commentBody" rows="10" cols="20"></textarea><br />
<input type="button" id="btnsummit" value="提交" onclick="atPost();" />
</div>
</form>
JS檔案:
代碼
function atPost(){
var commentTitle=$("#commentTitle").val();
var commentBody =$("#commentBody").text();
$.ajax({
type:"POST",
url:"atPost.aspx",
data:"commentTitle="+commentTitle+"&commentBody="+commentBody,
dataType:"json",
error:function(){
alert("未知錯誤!");
},
success:function(data){
var msg ="未知錯誤!";
switch(data.result){
case 0:
msg="提交失敗!";
break;
case 1:
msg="提交成功!";
break;
case 2:
msg="Code錯誤!";
break;
default:
msg=data.result;
break;
}
alert(msg);
}
});
}
atPost.aspx.cs代碼:
代碼
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NT.Common;
using NT.Entity;
using NT.Bll;
public partial class atPost : System.Web.UI.Page
{
protected string _title = "";
protected string _content = "";
/// <summary>
/// Status
/// 0 提交失敗
/// 1 提交成功
/// </summary>
protected int _StausCode = 0;
protected void Page_Load(object sender, EventArgs e)
{
_title = NTRequest.GetForm("commentTitle");
_content = NTRequest.GetForm("commentBody");
if ()
_StausCode = 1;
else
_StausCode = 0;
///返回Json格式
Response.ContentType = "text/html";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write("{\"result\":"+_StausCode+"}");
Response.End();
}
public bool
{
WXCommentInfo comment = new WXCommentInfo();
comment.CommentTitle = _title.ToString();
comment.CommentContent = _content.ToString();
comment.Articleid = 1;
comment.ArticleTitle = "";
comment.AddTime = DateTime.Now;
comment.Classid = 1;
comment.ClassName = "";
comment.IsPass = false;
comment.Target = 0;
comment.IP = Request.UserHostAddress;
comment.UserID = "xxp";
return Comment.AddComment(comment);
}
}