一個簡單的ajax操作

來源:互聯網
上載者:User

由於公司接下來的項目 會不斷用到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);
}
}

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.