Accumulated [JS]-Ajax value transfer (Layer 3) Notes

Source: Internet
Author: User

Ajax value transfer (three layers ):

1. Write in front-end page js to obtain the page data, pass the value to. CS, and return the value to determine whether the page is successful.

Code:

 1   <  Script  Language  = "JavaScript"  >  2       VaR  Title  =  $ (  " # Tb1  "  ). Val ();  3       VaR  Contents  =  $ (  "  # Tb2  "  ). Val (); 4   $. Ajax ({  5   Type:  "  Post  "  ,  6   Datatype:  "  Josn  " ,  7   Data: {Jian: title, jian2: Contents },  8   Success:  Function  (Data ){  9               If  (Data. Result  =  " 0  "  {  10   Alert (  "  Failed to add  "  );  11   }  12               Else {  13   Alert (  "  Added  "  );  14   }  15   },  16   Error: Function  (Data ){  17   Alert (  "  Error  "  );  18   }  19   })  20  21   </  Script  > 

2. In the background. CS, the aim is to pass the data obtained from the foreground to BLL.

Code:

 1   Protected   Void Bt1_click ( Object  Sender, eventargs E)  2   {  3               String Title = context. request [ "  Jian " ]; //  These two statements store the values obtained in front-end Js.  4               String Conte = context. request [ "  Jian2  "  ];  5               Int Result = New Bll. articlesmanager. Add (title, content ); //  Transmits a value to BLL and returns it.  6 Response. Write ( "  {\ "Result \":\"  " + Result + "  \"}  "  ); Returns the obtained value.  7               //  "{" Key ":" value "}" is in this format. If not, it must be transferred.  8               //  "{\" Result \ ": \" "+ Result + "\"}"  9 }

3. Obtain the value in. CS in BLL and pass it to Dal.
Code:

1 Public Static IntAdd (StringTitle,StringContent)2 {3Return NewDal. articlesservice. Add (title, content );4}

4. Adding data in the dal is the same as normal,

Code:

 1   Public   Static   Int Add (String Title, String  Content)  2   {  3               String SQL = "  Insert into articles (title, contents) values (@ title, @ contents)  "  ;  4 Sqlparameter [] par = New  Sqlparameter [] {  5                  6                   //  New sqlparameter ("@ ID", articels. ID ),  7                   New Sqlparameter ( "  @ Title  "  , Title ),  8                   New Sqlparameter ( "  @ Contents  "  , Content)  9  };  10               Return  Dbhelper. adddeed (SQL, par );  11 }

 

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.