$. Ajax solution for JSP jQuery failure

Source: Internet
Author: User

The code for my page is as follows, there is no problem with the jquery-1.4.2.min.js import, the file and jquery-1.4.2.min.js were originally placed under the webroot directory and can run normally.
But I put these two files together under a folder named test $. ajax (...) just like no execution, the server does not respond at all (no access to TestServlet at all). The address bar of the browser only adds a # number at the end of the path, which is unchanged, that is to say, the page is only called back to this page. Both alert ("in test"); and alert ("end"); are executed, that is, $. ajax is not executed.

<Head>
<Script Charset = "UTF-8" type = 'text/javascript 'src = 'jquery-1.4.2.min.js'> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
Var age = 0;
Alert ("in test ");
$ ("A"). click (function (){
Alert ("click ");
$. Ajax ({
Type: "GET", // http Request Method
Url: "TestServlet", // server url
Data: "name = weager" + "& age =" + age, // send data to the server
DataType: "json", // inform JQuery of the returned data format (such as xml, json, jsonp, script, etc)
Success: callback // call the callback function when the data returned after the request operation is complete. The ajax parameters include complete, success, and error. Complete refers to readystate = 4; success refers to state = 200; error refers to an error or a parsing server data error.
// Error: function (data) {alert (data );}
});
Alert ("end ");
});
});

Function callback (data ){
Alert ("call back called! ")
Alert (data. age );
$ ("Div" pai.html (data. age );
}
</Script>

There are very few questions about this on the Internet. Because there is no change on the page and the server has no response, the $. ajax method is not self-written, so it is very difficult to find a bug. Later forced, only to debug deep into jquery, but there is no line feed in the jquery-1.4.2.min.js, debugging and viewing code is very difficult, so I downloaded an uncompressed version of The jquery-1.4.2.js to replace the jquery-1.4.2.min.js. Then use ie8 for debugging (firebug can also be used). After debugging, check the responseText attribute of the xhr object in line 3 to find that the server-side error is 5179 (... test/TestServlet is not avalible), so that the server cannot find the matching url when looking for the Servlet.

So I put the web. the url-pattern in xml can be changed to test/TestServlet, but a similar error occurs when the jsp file outside the test folder accesses the same Servlet, to set basePath on the jsp page (basePath is used to set the path prefix of the page), you can remove the folder name on the url.

The specific method is as follows:

1. the url-pattern in the web. xml file does not need to be modified, so you can keep the original one.

2. Add the following before
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
3. Add <base href = "<% = basePath %> in
OK ~~ Once the problem is fixed, it is easy to solve the problem as long as the error can be found. The most feared thing is that the error cannot be seen and the operation is abnormal!

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.