javascript Jquery ajax 调用未命中 servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16269499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:59:30  来源:igfitidea点击:

Jquery ajax call is not hitting servlet

javajavascriptjqueryajax

提问by Justin

I am trying to make a simple ajax call. No matter what I do, it always executes the error block. I have a sysout in the doPost that is never hit. Someone please tell me what I am doing wrong. Here is my code.

我正在尝试进行简单的 ajax 调用。不管我做什么,它总是执行错误块。我在 doPost 中有一个从未被击中的系统输出。有人请告诉我我做错了什么。这是我的代码。

javascript----

javascript----

$.ajax({
    url: "GetBulletAjax",
    dataType: 'json',
    success: function(data) {
        alert("success");
    },
     error: function(jqXHR, textStatus, errorThrown) {
        alert(jqXHR+" - "+textStatus+" - "+errorThrown);
    }       
}); 

Java----

爪哇——

public class GetBulletAjax extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public GetBulletAjax() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("made it to servlet");
        PrintWriter out = response.getWriter(); 
        User user = (User) request.getSession().getAttribute("user");
        int userId = user.getId();
        List<Bullet> bullets;

        BulletDAO bulletdao = new BulletDAOImpl();
        try {
            bullets = bulletdao.findBulletsByUser(userId);
            Gson gson = new Gson();
            String json = gson.toJson(bullets);
            System.out.println(json);
            out.println(json);
            out.close();

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }

}

web.xml----

web.xml----

<servlet>
    <servlet-name>GetBulletAjax</servlet-name>
    <servlet-class>bulletAjax.GetBulletAjax</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>GetBulletAjax</servlet-name>
    <url-pattern>/GetBulletAjax</url-pattern>
</servlet-mapping>

采纳答案by David Hoerster

What's the URL for your client? Your URL is going to be relative -- so if your page's URL is <server>/foo/bar.html, your ajax request is going to go to <server>/foo/GetBulletAjax. But your servlet definition is <server>/GetBulletAjax.

你客户的网址是什么?你的 URL 将是相对的——所以如果你的页面的 URL 是<server>/foo/bar.html,你的 ajax 请求将转到<server>/foo/GetBulletAjax。但是您的 servlet 定义是<server>/GetBulletAjax.

Change your urlin your ajax request to /GetBulletAjax. You need the leading forward slash to tell the browser the resource is located off the root of the site.

url您的 ajax 请求更改为/GetBulletAjax. 您需要前导正斜杠来告诉浏览器资源位于站点的根目录之外。

回答by Sanath

in Jquery documentation

在 Jquery 文档中

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

type (default: 'GET') Type: String The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

type (default: 'GET') Type: String 要发出的请求类型(“POST”或“GET”),默认为“GET”。注意:这里也可以使用其他的 HTTP 请求方法,如 PUT 和 DELETE,但并非所有浏览器都支持。

seems that you miss the type attribute which needs to be POST. default is GET as mentioned by documentation. You dont have a doGet in your servlet to support that.

似乎您错过了需要 POST 的类型属性。默认为文档中提到的 GET 。您的 servlet 中没有 doGet 来支持它。

$.ajax({
   url: "GetBulletAjax",
   dataType: 'json',
   type:POST,
   success: function(data) {
      alert("success");
   },
   error: function(jqXHR, textStatus, errorThrown) {
      alert(jqXHR+" - "+textStatus+" - "+errorThrown);
   }       
});