Jquery Ajax submits a form from action to JSP

Source: Internet
Author: User
JSP page: JavaScript code
 
  1. VaR clienttel = $ ("# clienttel"). Val ();
  2. VaR activityid = $ ("# activityid"). Val ();
  3. $. Ajax ({
  4. Type: "Post", // sending Method
  5. URL: "/arweb/Reserve/savecode. Action", // path
  6. Data: "clienttel =" + clienttel + "& activityid =" + activityid,
  7. Success: function (text) {$ ("# randomcode"). Val (text );},
  8. Error: function (text) {alert ("sorry, the user ID does not exist. Please enter the correct user ID ");}
  9. });
VaR clienttel = $ ("# clienttel "). val (); var activityid = $ ("# activityid "). val (); $. ajax ({type: "Post", // sending Method URL: "/arweb/Reserve/savecode. action ", // path data:" clienttel = "+ clienttel +" & activityid = "+ activityid, success: function (text) {$ (" # randomcode "). val (text) ;}, error: function (text) {alert ("sorry, the user ID does not exist. Please enter the correct user ID ");}});

Action class: Java code

  1. Httpservletresponse res = servletactioncontext. getresponse ();
  2. Res. Reset ();
  3. Res. setcontenttype ("text/html; charset = UTF-8 ");
  4. Printwriter PW = res. getwriter ();
  5. PW. Print (random );
  6. PW. Flush ();
  7. PW. Close ();
Httpservletresponse res = servletactioncontext. getresponse (); Res. reset (); Res. setcontenttype ("text/html; charset = UTF-8"); printwriter PW = res. getwriter (); PW. print (random); PW. flush (); PW. close ();

PW. print (random); here the random is the value that action will pass to JSP. In JSP, success: function (text) here the text is the value that receives the data from the action.

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.