Javascript 如何解决不在控制台中显示的ajax问题?
我在JavaSpring引导应用程序中有一个简单的ajax调用,它调用控制器中的一个方法,然后将值返回到前端控制台。但是,当我运行代码时,它的运行状态为Javascript 如何解决不在控制台中显示的ajax问题?,javascript,java,ajax,Javascript,Java,Ajax,我在JavaSpring引导应用程序中有一个简单的ajax调用,它调用控制器中的一个方法,然后将值返回到前端控制台。但是,当我运行代码时,它的运行状态为400,但在我的控制台中没有显示任何内容。我不确定我是否忘记了什么或者我把它设置错了,但我假设没有任何东西被传递回去 JQuery: $(".modalPathContentBtn").on("click", function(event) { getSecondQuery(); });
400
,但在我的控制台中没有显示任何内容。我不确定我是否忘记了什么或者我把它设置错了,但我假设没有任何东西被传递回去
JQuery:
$(".modalPathContentBtn").on("click", function(event) {
getSecondQuery();
});
function getSecondQuery() {
var search2 = {
"dtoTiername" : "Test"
}
$.ajax({
type : "POST",
contentType : 'application/json; charset=utf-8',
dataType : 'json',
url : "/ajax/mqlGetSecondQuery",
data : JSON.stringify(search2),
success : function(result) {
console.log("It works: " + result);
}
});
}
@RequestMapping(value = "/ajax/mqlGetSecondQuery", method = RequestMethod.POST)
public @ResponseBody String sendSecondQuery(@RequestBody TestApp mTestApp, HttpServletRequest request) {
String pathVal = mTestApp.getDtoTiername();
System.out.println("Test of if it is getting to this part of the code");
return "randomString";
}
Java:
$(".modalPathContentBtn").on("click", function(event) {
getSecondQuery();
});
function getSecondQuery() {
var search2 = {
"dtoTiername" : "Test"
}
$.ajax({
type : "POST",
contentType : 'application/json; charset=utf-8',
dataType : 'json',
url : "/ajax/mqlGetSecondQuery",
data : JSON.stringify(search2),
success : function(result) {
console.log("It works: " + result);
}
});
}
@RequestMapping(value = "/ajax/mqlGetSecondQuery", method = RequestMethod.POST)
public @ResponseBody String sendSecondQuery(@RequestBody TestApp mTestApp, HttpServletRequest request) {
String pathVal = mTestApp.getDtoTiername();
System.out.println("Test of if it is getting to this part of the code");
return "randomString";
}
您提到您的请求失败,状态代码为400,这意味着ajax请求的
success
不会被调用,因为请求没有成功。您需要添加一个失败处理程序。它看起来像这样
$.ajax({
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: "/ajax/mqlGetSecondQuery",
data: JSON.stringify(search2),
success: function(result) {
console.log("It works: " + result);
},
fail: function(err) {
console.log(err)
}
});
这可能不是确切的语法,但这里的想法是您有一个失败处理程序,因此这是最奇怪的事情,但我添加了您的建议,但仍然没有显示任何内容。这是一个200结果代码,在我的java中使用了简单的
system.out.println()
,但是console.log()
仍然没有显示成功或错误。您使用了哪个SpringBoot启动器(或启动器)?