javaajax变量传递

javaajax变量传递,java,javascript,ajax,spring,spring-mvc,Java,Javascript,Ajax,Spring,Spring Mvc,我有一个java文件,我想通过它使用AJAX将类似于:{id:5,GPA:5}的映射传递到我的jsp文件。我正在为此使用以下代码: 在我的JAVA文件中: protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { JSONObject jsonResult = new JSONObject

我有一个java文件,我想通过它使用AJAX将类似于:{id:5,GPA:5}的映射传递到我的jsp文件。我正在为此使用以下代码:

在我的JAVA文件中:

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id", "5");
            jsonResult.put("GPA", "5");

            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonResult.toString());
}
在jsp文件中: --一些extJS代码--

response.responseText正在打印整个jsp文件,而不是打印id:5、GPA:5 有人能帮我吗

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id", "5");
            jsonResult.put("GPA", "5");

            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonResult.toString());
}
这将无法编译,您缺少return语句


根据
ModelAndView
返回类型判断,这似乎是一个Spring MVC控制器。我猜您返回的是一个JSP视图,而不是想要返回的JSON对象。有关如何从Spring MVC返回JSON对象的信息,请参阅。

您的函数不返回任何内容,这是正确的,因此请将其更改为void:

protected void handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id", "5");
            jsonResult.put("GPA", "5");

            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonResult.toString());
}

重新标记,因为您似乎正在使用Spring MVCI。很抱歉,我添加了返回语句ModelAndView mav=new ModelAndView(“屏幕上的断言”、“AssertRules”、“A”);返回mav;
protected void handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id", "5");
            jsonResult.put("GPA", "5");

            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(jsonResult.toString());
}