加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何返回AJAX查询的纯文本?

发布时间:2020-12-16 02:55:22 所属栏目:百科 来源:网络整理
导读:我的基础是如何做到这一点 on this Crunchify tutorial. 我有一个单页面的应用程序. 它有两个功能.它需要向HTTP servlet发送请求,该servlet将调用自己的java,并从中接收包含任何错误的JSON字符串/建议servlet接下来要做什么. 另一个功能是它从servlet中提示
我的基础是如何做到这一点 on this Crunchify tutorial.

我有一个单页面的应用程序.

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

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

问题是 – 我如何构建我的servlet,以便它返回一个纯文本HTTP响应,以便检查AJAX查询.

我有一个很好的方法来做这个,我想建议如何以更简单的方式实现同??样的事情.

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.然后,servlet返回ViewName = text,ModelName = model的ModelAndView.

然后,调度程序重定向到/jsp/text.jsp(指定视图),在其上显示模型.最终呈现的输出将返回给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

现在转到需要返回纯文本的另一种方式.

使用@ResponseBody注释在控制器中注释您的submitQuery()方法:

@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",true);
    xmlhttp.send();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读