一个简单的ajax操作

由于公司接下来的项目 会不断用到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 (PostComment())
_StausCode
= 1;
else
_StausCode
= 0;

///返回Json格式
Response.ContentType = "text/html";
Response.ContentEncoding
= System.Text.Encoding.UTF8;
Response.Write(
"{\"result\":"+_StausCode+"}");
Response.End();
}

public bool PostComment()
{
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);
}
}

 

 

 

posted @ 2010-02-06 16:07  沙茶叶  阅读(489)  评论(0编辑  收藏  举报