加载页面就触发ajax,AJAX post方法,有时会在页面加载时触发,有时不会
我對AJAX有一個奇怪的問題,我在頁面加載時使用AJAX POST方法返回對象地圖。
我正在調試該過程,有時會調用該方法,并且Java Servlet有時會運行。
我正確地包含了JS導入,其他jQuery調用正常工作。
我試過不同的探險家。
我使用的是GET方法,但仍然相同。
以前有人遇到過同樣的問題嗎?感謝
我的代碼:
AJAX:
$(document).ready(function(){
// here there is another ajax servlet call, and it's working fine all time.
$.get("ServletName", function(responseJson) {
//do some action
});
});
的Servlet
package packageName; //I keep it like that without a subfolder name that contains the servlets, doing so not to import the java classes located in the original package...would this be the problem?
@WebServlet(name = "ServletName", urlPatterns = {"/ServletName"})
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Map objectsTree = new TreeMap<>();
//adding objects to the map
String mapString = new Gson().toJson(objectsTree);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(mapString);
}
我對我的文件位置有疑問,我的JSP文件位于&#34; root \ web \&#34;
我的selvlet位于root \ src \ java \ packageName \ servlets \
但是仍然。調用所有servlet并沒有問題,但只有這個調用沒有一直到達...如果我一次又一次地運行調試器,它會在一些嘗試中起作用。
總結
以上是生活随笔為你收集整理的加载页面就触发ajax,AJAX post方法,有时会在页面加载时触发,有时不会的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 绝地服务器维护7月5日,绝地求生正式服7
- 下一篇: ajax 页面无刷新,Ajax的页面无刷