Javascript 我不知道';我不知道为什么JSON响应可以';t forword ajax';s回拨,但通过浏览器显示,并停留在我发送请求的页面上
我正在使用AJAX和servlet制作web应用程序,现在我正在制作一些登录页面 虽然servlet以JSON的形式响应,但这个JSON数组只出现在浏览器上,(它仍然停留在请求url上),而不是转发到ajax。下面是我的代码 web.xmlJavascript 我不知道';我不知道为什么JSON响应可以';t forword ajax';s回拨,但通过浏览器显示,并停留在我发送请求的页面上,javascript,java,ajax,servlets,Javascript,Java,Ajax,Servlets,我正在使用AJAX和servlet制作web应用程序,现在我正在制作一些登录页面 虽然servlet以JSON的形式响应,但这个JSON数组只出现在浏览器上,(它仍然停留在请求url上),而不是转发到ajax。下面是我的代码 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmln
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>InfiniStorWeb</display-name>
<context-param>
<param-name>project</param-name>
<param-value>InfiniStorWeb!</param-value>
</context-param>
<servlet>
<servlet-name>other servlet</servlet-name>
<servletother servlet class</servlet-class>
</servlet>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>other servlet</servlet-name>
<url-pattern>*.ser</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.html
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>InfiniStorWeb</display-name>
<context-param>
<param-name>project</param-name>
<param-value>InfiniStorWeb!</param-value>
</context-param>
<servlet>
<servlet-name>other servlet</servlet-name>
<servletother servlet class</servlet-class>
</servlet>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>other servlet</servlet-name>
<url-pattern>*.ser</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
$(文档).on(就绪,函数(){
$('#frameSet',parent.document).attr('rows','0',*,0')
登录表单
<form id="formLogin" action="./login.do" method="POST">
<div id="wrap" style="padding-top:30px;">
<div style="width:350px; height:80px; font-family:Tahoma; font-size:35px; font-weight:bold; margin:0 auto;">
Login Page!
</div>
<div style="width:350px; height:40px; margin:0 auto;">
<div style="width:30px; float:left;"> </div>
<div style="font-family:Tahoma; font-size:15px; width:65px; float:left; margin-right:10px; text-align:left; padding-top:6px;">
Username
</div>
<div style="font-size:15px; width:210px; float:left;">
<input type="text" id="textAccount" name="textAccount" style="width:206px; height:26px;">
</div>
</div>
<div style="width:350px; height:50px; margin:0 auto;">
<div style="width:30px; float:left;"> </div>
<div style="font-family:Tahoma; font-size:15px; width:65px; float:left; margin-right:10px; text-align:left; padding-top:6px;">
Password
</div>
<div style="font-size:15px; width:210px; float:left;">
<input type="password" id="textPass" name="textPass" style="width:206px; height:26px;">
</div>
</div>
<div style="width:280px; height:30px; margin:0 auto;">
<div style="font-size:15px; width:210px; float:right; text-align:right; padding-right:0px;">
<input id="submitSend" type="submit" value="Sing in" style="background:#DDDDDD; border:1px solid; height:24px;">
</div>
</div>
<div style="width:450px; height:20px; margin:0 auto;">
</div>
</form>
登录页面!
用户名
密码
我希望会执行成功例程。但即使不调用错误回调,浏览器也只显示json格式的字符串,并保留我发送请求的页面(在上面的例子中,它保持“login.do”)
我该怎么办
EDIT:index.html javascript代码已编辑,我添加了我的登录表单。您的表单正在正常提交。请阻止它,然后调用ajax
$('#formLogin').submit(function(e) {
e.preventDefault();//prevent form submit
var sendData = $(this).serializeArray();
var url = $(this).attr("action");
//ajax call and rest...
})
您的表单正在正常提交。请阻止它,然后调用ajax
$('#formLogin').submit(function(e) {
e.preventDefault();//prevent form submit
var sendData = $(this).serializeArray();
var url = $(this).attr("action");
//ajax call and rest...
})
编辑:
将您的登录表单更改为
<form id="formLogin" onsubmit="return submitForm()" method="POST">
---------------------
---------------------
---------------------
</fomr>
---------------------
---------------------
---------------------
将js更改为:
<script>
function submitForm(){
//your ajax code here
return false;
}
</script>
函数submitForm(){
//这里是您的ajax代码
返回false;
}
编辑:
将您的登录表单更改为
<form id="formLogin" onsubmit="return submitForm()" method="POST">
---------------------
---------------------
---------------------
</fomr>
---------------------
---------------------
---------------------
将js更改为:
<script>
function submitForm(){
//your ajax code here
return false;
}
</script>
函数submitForm(){
//这里是您的ajax代码
返回false;
}
尝试在提交函数中添加e.preventDefault()
,因为表单正在提交,您的ajax函数从未调用过。请您也添加您的formLogin表单好吗?@anu很抱歉,代码已经包含,但我忘记了关于我问题的帖子。我edited@lgorovics我发帖了。谢谢。尝试添加e.preventDefault()
在提交函数中由于表单即将提交,您的ajax函数从未调用。请您也添加您的formLogin表单好吗?@anu很抱歉,代码已经包含,但我忘记了关于我问题的帖子。我edited@lgorovics我发帖了。谢谢。仍然只有浏览器显示json字符串。现在调用成功方法…怎么了?仍然只有浏览器显示json字符串..现在调用成功方法…怎么了??