jquery $.get (), $.ajax () and background servlet interaction methods

Source: Internet
Author: User
<span id="Label3"></p><p><p>The load () method in jquery usually gets static files from the Web server, and if you need to pass some parameters to a page in the server, you can use either $.get () or $.post () or the underlying $.ajax () method.</p></p><p><p>The $.get (url, data, callback, type) parameters are described below:</p></p><p><p>1.url: the requested Url.</p></p><p><p>2.data: Key/value data sent to the server is appended to the Url.</p></p><p><p>3.callback: the callback function when loading succeeds (called when response returns a status of Success) automatically passes the request result and state to the Method.</p></p><p><p>4. The server returns the format of the Content.</p></p><p><p>Example: Click a button to upload a string from the client to the server and return the new string after processing by the Server.</p></p><p><p>JSP Code:</p></p><pre class="brush:html;toolbar:false"><pre class="brush:html;toolbar:false"><body><input type= "button" id= "send" value= "ajax get"/><div id= "resText" ></div></body></pre></pre><p><p>JavaScript code:</p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false"><script type= "text/javascript" >$ (function () {$ ("#send"). click (function () {var str = "test"; $.get ("ajaxservlet" , {name:str}, function (responsetext, textstatus) {if (textstatus = = "success") $ ("#resText"). text (responsetext); if (tex Tstatus = = "error") Alert ("error");});}); </script></pre></pre><p><p>Create the servlet class Ajaxservlet,doget () function code as Follows:</p></p><pre class="brush:java;toolbar:false"><pre class="brush:java;toolbar:false">protected void doget (httpservletrequest request, httpservletresponse Response) throws servletexception, IOException { String name = Request.getparameter ("name"); response.setcontenttype ("text/html"); PrintWriter writer = Response.getwriter (); writer.println ("helloworld" + name);}</pre></pre><p><p>Servlet configuration:</p></p><pre class="brush:xml;toolbar:false"><pre class="brush:xml;toolbar:false"><servlet> <servlet-name>AjaxServlet</servlet-name> <servlet-class>org. ajaxservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ajaxservlet</ Servlet-name> <url-pattern>/AjaxServlet</url-pattern> </servlet-mapping></pre></pre><p><p><br></p></p><p><p>Start the program and click the button to return a new string</p></p><p><p><br></p></p><p><p>The $.ajax () method is the Lowest-level Ajax implementation of jquery, so it can be used instead of the $.get () method.</p></p><p><p>In the example above, the JS code becomes:</p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">$ (function () {$ ("#send"). Click (function () {var ajaxdata = {name: "xiaoli", psd: "123"};$.ajax ({url: "ajaxservlet", type: "GET", data:ajaxdata,success:function (responsetext, textstatus) {$ ("#resText"). text (responsetext);},error: function () {alert ("error");}});});</pre></pre><p><p><br></p></p><p><p>jquery $.get (), $.ajax () and background servlet interaction methods</p></p></span>

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.