gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:25:57 25 4
gpt4 key购买 nike

我的知识基于如何做到这一点 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com