A simple Ajax operation

Source: Internet
Author: User

Since the company will continue to use Ajax for subsequent projects!

So I studied it carefully.

 

The basic operation is quite simple!

 

HTML:

Code

 <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 = "Submit" onclick = "atpost ();"/>
</Div>
</Form>

 

JS file:

Code

 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 ("Unknown error! ");
},
Success: function (data ){
VaR MSG = "Unknown error! ";

Switch (data. Result ){
Case 0:
MSG = "failed to submit! ";
Break;
Case 1:
MSG = "submitted successfully! ";
Break;
Case 2:
MSG = "code error! ";
Break;
Default:
MSG = data. result;
Break;
}
Alert (MSG );
}
});
}

 

Atpost. aspx. CS code:

Code

 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 failed to submit
/// 1 submitted successfully
/// </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;

/// Return the JSON format
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 );
}
}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.