zoukankan      html  css  js  c++  java
  • 一个简单的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);
    }
    }
  • 相关阅读:
    刚加入博客园
    个人作业——软件工程实践总结作业
    前四次作业--个人总结
    项目选题报告(待就业六人组)
    结对第二次—文献摘要热词统计及进阶需求
    结对第一次—原型设计(文献摘要热词统计)
    第一次作业
    logback-spring.xml 配置说明
    k8s 微服务打包上传私库、部署、发布
    k8s离线安装监控Kubernetes集群
  • 原文地址:https://www.cnblogs.com/cancer_xu/p/1665014.html
Copyright © 2011-2022 走看看