在SpringMVC响应中获取AJAX HTTP 500

时间:2018-09-23 10:03:03

标签: ajax spring-mvc

我正在尝试使用ajax从Mongodb中获取用户列表,该列表在没有ajax的情况下也有效。我使用MySQL做到了这一点,并且可以正常工作,所以我不明白我的代码有什么问题。

我可以发送请求并转到方法,然后返回List<User>。 但是,当我将其放入map中时,无法将该地图作为成功函数中的响应。

我的控制台输出:

free.js:31 Uncaught TypeError: Cannot read property 'action' of null
    at window.onmessage (free.js:31)
window.onmessage @ free.js:31
jquery.min.js:2 POST http://localhost:8080/UserManagementMongoDB/list 500
send @ jquery.min.js:2
ajax @ jquery.min.js:2
load @ submitedform:342
onload @ submitedform:25
submitedform:355 Uncaught ReferenceError: response is not defined
    at Object.error (submitedform:355)
    at u (jquery.min.js:2)
    at Object.fireWith [as rejectWith] (jquery.min.js:2)
    at k (jquery.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2)

这是我的Controller方法

@RequestMapping(value="/list", method=RequestMethod.POST)
public @ResponseBody Map<String,Object> getAll(User user) {
    Map<String,Object> map = new HashMap<String,Object>();

        List<User> list = mainPageService.getAllUsers();

        if (list != null){
            map.put("status","200");
            map.put("message","Data found");
            map.put("data", list);
        }else{
            map.put("status","404");
            map.put("message","Data not found");

        }

    return map;
}

还有我的Ajax加载功能

data = "";
load = function(){  
    $.ajax({
        url:'list',
        type:'POST',
        success: function(response){
            alert("Hello");
                data = response.data;
                $('.tr').remove();
                for(i=0; i<response.data.length; i++){                  
                    $("#table").append("<tr class='tr'> <td> "+response.data[i].userName+" </td> <td> "+response.data[i].userSurname+" </td> <td> <a href='#' onclick= edit("+i+");> Edit </a>  </td> </td> <td> <a href='#' onclick='delete_("+response.data[i].userId+");'> Delete </a>  </td> </tr>");

                }           
        },
        error: function () {
            alert(response);    
          }

    });

这是我的依赖项

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>UserManagementMongoDB</groupId>
  <artifactId>UserManagementMongoDB</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>UserManagementMongoDB Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    
  <!-- Servlet Library -->
    <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${javax.servlet-api.version}</version>
        <scope>provided</scope>
    </dependency>
    
  <!-- Spring dependencies -->
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.framework.version}</version>
    </dependency>

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.framework.version}</version>
    </dependency>

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.framework.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.framework.version}</version>
    </dependency>
    
    <dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-commons</artifactId>
  <version>${spring.data.commons.version}</version>
 </dependency>
 
 <dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-mongodb</artifactId>
  <version>${spring.data.mongodb.version}</version>
 </dependency>
    
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>${jstl.version}</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/net.tanesha.recaptcha4j/recaptcha4j -->
<dependency>
    <groupId>net.tanesha.recaptcha4j</groupId>
    <artifactId>recaptcha4j</artifactId>
    <version>0.0.7</version>
</dependency>
    
    <!-- https://mvnrepository.com/artifact/org.glassfish/javax.json -->
		<dependency>
			<groupId>org.glassfish</groupId>
			<artifactId>javax.json</artifactId>
			<version>1.1.2</version>
		</dependency>
    
    <dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-mapper-asl</artifactId>
	<version>1.9.10</version>
</dependency>

<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.3</version>
</dependency>


<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-tx</artifactId>
	<version>${spring.framework.version}</version>
</dependency>
    
</dependencies>



  <!-- Properties for the dependencies -->
  <properties>
  
  <java-version>1.7</java-version>
   <!-- Spring properties -->
   
   <spring.framework.version>5.0.9.RELEASE</spring.framework.version>
   <spring.data.commons.version>1.12.1.RELEASE</spring.data.commons.version>
   <spring.data.mongodb.version>1.10.6.RELEASE</spring.data.mongodb.version>
   
   <!-- Other properties -->
   <javax.servlet-api.version>3.1.0</javax.servlet-api.version>
   <junit.version>4.12</junit.version>
   <jstl.version>1.2</jstl.version>
  </properties>
  
  <build>
    <finalName>ExampleWebApplication</finalName>
  </build>
</project>

我现在将获得500分,但我无法获得其他所有响应。我可以用ajax发送参数,只是无法接收地图资源

我的模型代码:

public class User {

@Id
private String userId;

private String userName;
private String userSurname;
private String phoneNo;

public User(String userId,String userName,String userSurname,String phoneNo) {
    setUserId(userId);
    setUserName(userName);
    setUserSurname(userSurname);
    setPhoneNo(phoneNo);
}


public User() {
    // TODO Auto-generated constructor stub
}

public String getUserId() {
    return userId;
}
public void setUserId(String userId) {
    this.userId = userId;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public String getUserSurname() {
    return userSurname;
}
public void setUserSurname(String userSurname) {
    this.userSurname = userSurname;
}
public String getPhoneNo() {
    return phoneNo;
}
public void setPhoneNo(String phoneNo) {
    this.phoneNo = phoneNo;
}

我的Stacktrace:

  

ava.lang.Exception       在com.kaan.springmvc.controller.MainPageController.getAll(MainPageController.java:117)       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处       在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)       在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)       在java.lang.reflect.Method.invoke(未知来源)       在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)       在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)       在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)       在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891)       在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)       在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)       在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)       在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)       在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)       在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:660)       在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)       在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)       在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:770)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1415)       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)       在java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)       在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)       在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(未知来源)   引用

1 个答案:

答案 0 :(得分:-1)

您将前端和后端分开吗?

如果是,则可能是不同的服务器或端口号,并且违反了CORS。请帮助更新更多信息。

第二点,您的方法需要User对象,但是我对ajax请求没有任何要求吗?