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中有一个从未被命中的sysout。有人请告诉我我做错了什么。这是我的代码。
javascript----
JavaScript的----
$.ajax({
url: "GetBulletAjax",
dataType: 'json',
success: function(data) {
alert("success");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR+" - "+textStatus+" - "+errorThrown);
}
});
Java----
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>
2 个解决方案
#1
4
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将是相对的 - 因此,如果您的页面的URL是
Change your url
in 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.
将您的ajax请求中的网址更改为/ GetBulletAjax。您需要前导斜杠来告诉浏览器资源位于站点根目录之外。
#2
1
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(默认值:'GET')类型: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的type属性。默认是文档中提到的GET。你的servlet中没有doGet来支持它。
$.ajax({
url: "GetBulletAjax",
dataType: 'json',
type:POST,
success: function(data) {
alert("success");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR+" - "+textStatus+" - "+errorThrown);
}
});
#1
4
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将是相对的 - 因此,如果您的页面的URL是
Change your url
in 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.
将您的ajax请求中的网址更改为/ GetBulletAjax。您需要前导斜杠来告诉浏览器资源位于站点根目录之外。
#2
1
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(默认值:'GET')类型: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的type属性。默认是文档中提到的GET。你的servlet中没有doGet来支持它。
$.ajax({
url: "GetBulletAjax",
dataType: 'json',
type:POST,
success: function(data) {
alert("success");
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR+" - "+textStatus+" - "+errorThrown);
}
});