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 );
}
}