Javascript SpringMVC中Ajax请求的404实现
我正在尝试制作一个简单的登录表单。在这个例子中,我使用Spring MVC,为APIResponseModel创建了一个简单的类(这个类包含三个变量,有getter和setter,分别是Status、Message和HTTPStatus),当我填写凭证并单击submit时,我得到的ajax响应是404,但当我在控制器中选中同一个时,它返回Javascript SpringMVC中Ajax请求的404实现,javascript,ajax,forms,spring-mvc,authentication,Javascript,Ajax,Forms,Spring Mvc,Authentication,我正在尝试制作一个简单的登录表单。在这个例子中,我使用Spring MVC,为APIResponseModel创建了一个简单的类(这个类包含三个变量,有getter和setter,分别是Status、Message和HTTPStatus),当我填写凭证并单击submit时,我得到的ajax响应是404,但当我在控制器中选中同一个时,它返回{“Status”:“200”,“message:“Login Success!!”,“data”:Admin}同样,我在Ajax响应中获得404作为状态。如果
{“Status”:“200”,“message:“Login Success!!”,“data”:Admin}
同样,我在Ajax响应中获得404作为状态。如果您能帮助我,请在这里附上代码,并让我知道我在这里做错了什么
弹簧控制器
@RequestMapping("/login")
public APIResponseModel getLoginPage(@ModelAttribute("loginUser")UserDTO userDto, HttpSession session) {
APIResponseModel apiResponseModel = new Utils().getDefaultApiResponse();
logger.info("USER DTO :" + userDto.toString());
List<String> errorList = new ArrayList<>();
try {
userService = new UserServiceImpl();
UserModel userModel = userService.validateLogin(userDto,null, errorList);
if(errorList.isEmpty()) {
apiResponseModel.setMessage("Login Success!!");
apiResponseModel.setStatus(HttpStatus.OK);
if(userModel.getRole() == Constants.UserRole.ADMIN) {
apiResponseModel.setData("Admin");
}else if(userModel.getRole() == Constants.UserRole.STORE_MANAGER) {
apiResponseModel.setData("StoreOwner");
}else {
apiResponseModel.setData("landing");
}
session.setAttribute("loggedInUser", userModel);
}else {
apiResponseModel.setMessage(errorList.isEmpty() ? "Invalid Credientials !!" : errorList.toString());
}
}catch(Exception exe) {
exe.printStackTrace();
logger.info("Exception occured at : " + exe.getMessage());
}
logger.info("------------------------------------------------------------------------");
logger.info(apiResponseModel);
return apiResponseModel;
}
MasterAjax类
class MasterAjax{
constructor(){
this.requestType = null;
this.url = null;
this.timeout = 100000;
this.enctype = null;
this.data = null;
this.processData = null;
this.contentType = null;
this.responseData = null;
this.responseStatus = null;
this.responseStatusCode = null;
}
requestData(callBack){
var parameterError=false;
if(null == this.requestType){
parameterError=true;
console.log("Error: Request Type can't be null");
}
if(null === this.url || undefined === this.url || "undefined" === this.url){
parameterError=true;
console.log("Error: URL can't be null");
}
if(null == this.data || this.data.length <= 0){
//console.log("Warning: Data is null");
}
if(parameterError === false){
/*toggleSpinnerOn(); */
$.ajax({
type : this.requestType,
enctype : this.enctype,
processData : this.processData,
contentType : this.contentType,
url : global_contextPath+"/"+this.url,
data: this.data,
timeout : this.timeout,
success : function(responseData,textStatus) {
/*toggleSpinnerOff();*/
callBack(responseData,textStatus);
},
error : function(responseData,textStatus) {
/*toggleSpinnerOff(); */
callBack(responseData,textStatus);
}
});
}
//return this.responseData;
}
}
class MasterAjax{
构造函数(){
this.requestType=null;
this.url=null;
这个超时=100000;
this.enctype=null;
this.data=null;
this.processData=null;
this.contentType=null;
this.responseData=null;
this.responseStatus=null;
this.responseStatusCode=null;
}
请求数据(回调){
var参数error=false;
if(null==this.requestType){
参数error=true;
log(“错误:请求类型不能为null”);
}
if(null==this.url | | undefined===this.url | |“undefined”==this.url){
参数error=true;
log(“错误:URL不能为空”);
}
如果(null==this.data | | this.data.lengthMissed@ResponseBody
要在控制器上添加,这就是为什么它不在调用时发送响应
class MasterAjax{
constructor(){
this.requestType = null;
this.url = null;
this.timeout = 100000;
this.enctype = null;
this.data = null;
this.processData = null;
this.contentType = null;
this.responseData = null;
this.responseStatus = null;
this.responseStatusCode = null;
}
requestData(callBack){
var parameterError=false;
if(null == this.requestType){
parameterError=true;
console.log("Error: Request Type can't be null");
}
if(null === this.url || undefined === this.url || "undefined" === this.url){
parameterError=true;
console.log("Error: URL can't be null");
}
if(null == this.data || this.data.length <= 0){
//console.log("Warning: Data is null");
}
if(parameterError === false){
/*toggleSpinnerOn(); */
$.ajax({
type : this.requestType,
enctype : this.enctype,
processData : this.processData,
contentType : this.contentType,
url : global_contextPath+"/"+this.url,
data: this.data,
timeout : this.timeout,
success : function(responseData,textStatus) {
/*toggleSpinnerOff();*/
callBack(responseData,textStatus);
},
error : function(responseData,textStatus) {
/*toggleSpinnerOff(); */
callBack(responseData,textStatus);
}
});
}
//return this.responseData;
}
}
<form class="page" name="loginForm">
<input class="email" id="email" path="email" type='text' align="center" placeholder="Username / Email" />
<input class="pass" id="password" path="password" type='password' align="center" placeholder="Password" />
<button class="submit" onclick="processLogin(event)">Sign in</button>
<p class="forgot" align="center"><a href="#">Forgot Password?</a></p>
</form>