gpt4 book ai didi

java - Jquery ajax 调用没有命中 servlet

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:10:08 24 4
gpt4 key购买 nike

我正在尝试进行简单的 ajax 调用。无论我做什么,它总是执行错误 block 。我在 doPost 中有一个从未被击中的 sysout。有人请告诉我我做错了什么。这是我的代码。

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----

<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>

最佳答案

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

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

关于java - Jquery ajax 调用没有命中 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16269499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com