如何从jQuery $ .ajax调用servlet而不出现HTTP 404错误 [英] How to call servlet from jQuery $.ajax without getting HTTP 404 error

查看:372
本文介绍了如何从jQuery $ .ajax调用servlet而不出现HTTP 404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ajax调用来调用servlet,如下所示:

I am trying to call a servlet using ajax call as below:

$.ajax({
  url: 'CheckingAjax',
  type: 'GET',
  data: { field1: "hello", field2 : "hello2"} ,
  contentType: 'application/json; charset=utf-8',
  success: function (response) {
    //your success code
    alert("success");
  },
  error: function (errorThrown) {
    //your error code
    alert("not success :"+errorThrown);
  }                         
});

但是,它转到error功能并显示警报:

However, it goes to error function and shows alert:

未成功:未找到

not success :Not Found

这是怎么引起的,我该如何解决?

How is this caused and how can I solve it?

推荐答案

当您指定相对URL(不是以scheme或/开头的URL)时,它将相对于当前请求URL(您所使用的URL)在浏览器的地址栏中查看).

When you specify a relative URL (an URL not starting with scheme or /), then it will become relative to the current request URL (the URL you see in browser's address bar).

您告诉您的servlet在以下位置可用:

You told that your servlet is available at:

http://localhost:8080/FullcalendarProject/CheckingAjax

想象一下,运行ajax脚本的网页是通过以下方式打开的:

Imagine that the web page where your ajax script runs is opened via:

http://localhost:8080/FullcalendarProject/pages/some.jsp

然后您指定相对URL url: "CheckingAjax",那么它将被解释为:

And you specify the relative URL url: "CheckingAjax", then it will be interpreted as:

http://localhost:8080/FullcalendarProject/pages/CheckingAjax

但这不存在.因此,它将返回HTTP 404找不到页面"错误.

But this does not exist. It will thus return a HTTP 404 "Page Not Found" error.

为了使其正常工作,您基本上需要使用以下方式之一指定URL:

In order to get it to work, you basically need to specify the URL using one of below ways:

  1. url: "http://localhost:8080/FullcalendarProject/CheckingAjax"

这是不可移植的.每次将Web应用程序移至另一个域时,都需要对其进行编辑.您无法从Web应用程序内部进行控制.

This is not portable. You'd need to edit it everytime you move the webapp to another domain. You can't control this from inside the webapp.

url: "/FullcalendarProject/CheckingAjax"

这也不是真正可移植的.每次更改上下文路径时,都需要对其进行编辑.您无法从Web应用程序内部进行控制.

This is also not really portable. You'd need to edit it everytime you change the context path. You can't control this from inside the webapp.

url: "../CheckingAjax"

尽管您可以从webapp内部完全控制它,但它实际上也不是可移植的.每次在JSP中移动到另一个文件夹时,都需要对其进行编辑,但是,如果在JSP中移动,则基本上已经在忙于编码,因此可以轻松地同时进行.

This is actually also not portable, although you can fully control this from inside the webapp. You'd need to edit it everytime you move around JSP into another folder, but if you're moving around JSPs you're basically already busy coding, so this could easily be done at the same time.

最好的方法是让JSP EL动态打印当前请求上下文路径.假定JS代码包含在JSP文件中:

Best way would be to let JSP EL dynamically print the current request context path. Assuming that the JS code is enclosed in JSP file:

url: "${pageContext.request.contextPath}/CheckingAjax"

或者将其封装在自己的JS文件中(好的做法!),然后创建一个全局JS变量:

Or when it's enclosed in its own JS file (good practice!), then create either a global JS variable:

<script>var contextPath = "${pageContext.request.contextPath}";</script>
<script src="yourajax.js"></script>

使用

url: contextPath + "/CheckingAjax"

或文档元素上的HTML5数据属性:

or a HTML5 data attribute on the document element:

<html data-contextPath="${pageContext.request.contextPath}">
    <head>
        <script src="yourajax.js"></script>

使用

url: $("html").data("contextPath") + "/CheckingAjax"

这篇关于如何从jQuery $ .ajax调用servlet而不出现HTTP 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆