ajax - 如何为 AJAX 查询返回纯文本?

标签 ajax spring spring-mvc servlets single-page-application

我的知识基于如何做到这一点 on this Crunchify tutorial .

我有一个单页应用程序。

它有两个功能。它需要向 HTTP servlet 发送一个请求,后者将调用自己的 java,并从中接收一个包含任何错误的 JSON 字符串/建议 servlet 下一步做什么。

另一个功能是它从 servlet 提示保存文件对话框。

问题是 - 我如何构建我的 servlet,以便它为 AJAX 查询返回纯文本 HTTP 响应以进行检查。

我有一个非常全面的方法,我想就如何以更简单的方式实现同​​样的事情提出建议。

web.xml

   <servlet>
        <servlet-name>MyServlet</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServlet</servlet-name>
        <url-pattern>/submitQuery</url-pattern>     
          <url-pattern>/saveFile
    </servlet-mapping>

MyServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="world.hello.myservlets" />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

MyServlet.java
package world.hello.myservlets;

@Controller
public class MyServlet{


    @RequestMapping("/submitQuery")
    public ModelAndView submitQuery()
    {       

        return new ModelAndView("text", "model", "hello world");
    }

}

/WEB-INF/jsp/text.jsp
{model}

index.html
<html>
<head>
<script>
function myAjax()
{
         xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                alert(xmlhttp.responseText)
                /*do something with the http response*/
            }     

          }


     xmlhttp=new XMLHttpRequest();
     xmlhttp.open("GET", "submitQuery", true);
     xml.send();
}
</script>
</head>
<body>
    <button onclick="myAjax()">Click me</button>
</body>
</html>

我的理解是当 /submitQuery URI 被发送,它被映射到 MyServlet小服务程序。然后 servlet 返回 ModelAndViewViewName = text, ModelName = model .

然后调度程序重定向到/jsp/text.jsp(指定的 View ),在其上显示模型。最终呈现的输出返回到 AJAX 对象,然后该对象可以按照自己的意愿访问它。

有没有更直接的方法来做到这一点?

最佳答案

是的,有更直接的方法可以做到这一点。

根据 crunchify您正在返回的教程 ModelAndView目的。这就是您在 text.jsp 上获取模型对象的原因

ModelAndView: It returning both model and view information from a controller. Holder for both Model and View in the web MVC framework. Note that these are entirely distinct. This class merely holds both to make it possible for a controller to return both model and view in a single return value.

More about ModelAndView

现在来看看您需要返回纯文本的另一种方式。

注释您的 submitQuery() Controller 中的方法 @ResponseBody注解:
@RequestMapping(value="/submitQuery")
@ResponseBody
public String submitQuery() {
    return "Response";
}

The @ResponseBody can be put on a method and indicates that the return type should be written straight to the HTTP response body (and not placed in a Model, or interpreted as a view name)



访问 javascript 中的参数。
function myAjax()
{
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            alert(xmlhttp.responseText);
            console.log(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", "submitQuery", true);
    xmlhttp.send();
}

关于ajax - 如何为 AJAX 查询返回纯文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29378660/

相关文章:

java - Spring MVC 注解配置

java - 测试 Ajax - 在 Apache Wicket 中提交表单

javascript - 而 ajax,异步 :true, 延迟

java - Spring 属性文件

java - 存储在 ModelMap 中的 HTML 标签不会被浏览器解释为 HTML

java - 如何在java应用程序运行时更新css文件

spring-mvc - 使用 Roo 在 Spring MVC 中防止批量分配

javascript - gmaps4rails map 在 ajax 调用时未完全显示在 Canvas 上

javascript - 使用 PHP 和 AJAX 插入数据时的问题

java - CharacterEncodingFilter 不在 Spring MVC 3.0 中翻译 unicode 文本