简体   繁体   English

在spring mvc中接收ajax调用中的json数组时出错

[英]error in recieving json array in ajax call in spring mvc

guys i am trying to receive a json array in ajax call in spring MVC it enters in the error function not the success .. here is my code web.xml 伙计们,我试图在Spring MVC的ajax调用中接收一个json数组,它进入错误函数而不是成功..这里是我的代码web.xml

 <servlet>  
 <servlet-name>sdnext</servlet-name>  
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
 <init-param>  
        <param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/sdnext-servlet.xml</param-value></init-param>  
 <load-on-startup>1</load-on-startup>  
 </servlet>  

<servlet-mapping>  
<servlet-name>sdnext</servlet-name>  
<url-pattern>/spring/</url-pattern>  
</servlet-mapping> 

<welcome-file-list>  
<welcome-file>index.jsp</welcome-file>  
</welcome-file-list>  
</web-app>  

index.jsp 的index.jsp

<button type="button" onclick="ajaxCall()">List Employees</button>
<script>
    var grid;
    ajaxCall = function() {
        $.ajax({
            url : '/Spring3HibernateApp1/spring/index',
            type : 'POST',
            dataType : 'json',
            error : function(that, e) {
                alert(e);
            },
            success : function(data) {
                alert(JSON.stringify(data.MyList));
                var newData = JSON.stringify(data.MyList);
                grid = new GridLibrary(newData);
                grid.display();

            }
        });
    }

EmployeeController EmployeeController

public class EmployeeController {

@Autowired
private EmployeeService employeeService;

@RequestMapping(value = "/spring/index")
public @ResponseBody void doPost(HttpServletResponse response) throws JSONException, IOException {
    PrintWriter out = response.getWriter();
    JSONObject obj = new JSONObject();
    JSONArray list = new JSONArray();
    list.put(employeeService.listEmployeess());
    obj.put("MyList", list);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    out.write(obj.toString());

    out.close();

}

}

it gives my jquery.min.js:4 POST http://localhost:8080/Spring3HibernateApp1/spring/index 404 (Not Found) although i tested the controller and it returns a valid json .. any help ? 它给了我的jquery.min.js:4 POST http:// localhost:8080 / Spring3HibernateApp1 / spring / index 404(Not Found)虽然我测试了控制器,它返回一个有效的json ..任何帮助?

将您的控制器更改为:
@RequestMapping(value = "/spring/index") public @ResponseBody JSONArray doPost(HttpServletResponse response) throws JSONException, IOException { JSONArray list = new JSONArray(); list.put(employeeService.listEmployeess()); return list; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM