Ajax返回状态码200,成功函数不触发(Ajax returns status code 200, success function not triggering)
关于这一点有很多话题,但没有一个是有帮助的。 一切正常,数据插入数据库,并且(我使用spring mvc作为后端)我的控制器也正确处理所有内容。
Ajax的代码;
$.ajax({
type: "POST",
url: "http://localhost:8080/NotePath/register",
data: "email=" + email + "&password=" + password + "&name=" + name,
succes: function(response){
$("#formMail, #formPass, #formUsername").val("");
$("#loginRegister, #whiteBg").fadeOut();
},
error: function(xhr,err){
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
alert("responseText: "+xhr.responseText);
}
});
控制器代码;
@RequestMapping(value = "/register")
public @ResponseBody String registerUser(@RequestParam(value = "email") String email, @RequestParam(value ="password") String password,
@RequestParam(value = "name") String name, Model model){
String status = "";
Boolean isEmailTaken = registerServiceImpl.isEmailTaken(email);
Boolean isNameTaken = registerServiceImpl.isNameTaken(name);
if(isEmailTaken == false && isNameTaken == false){
try {
registerServiceImpl.registerUser(email, password, name);
status = "Succes";
} catch (Exception e) {
status = "Error";
e.printStackTrace();
}
}else if(isEmailTaken == true){
status = "mail taken";
}else if(isNameTaken == true){
status = "name taken";
}
return status;
}
我甚至尝试过返回responseEntity;
return new ResponseEntity(status, respHeader, HttpStatus.OK);
当我查看我的回复标题(使用谷歌浏览器)时,它甚至会说“邮件采取”或“采取名称”或应该出现的任何消息。 但由于某种原因,ajax成功功能没有被触发......
奇怪的是,我有其他ajax调用,没有任何问题。
我认为获得200的状态代码意味着一切都......好吧......好吧:p
There are quite a few topics about this, but none of them were any help. Everything is working fine, the data gets inserted into the db, and (i'm using spring mvc as back-end) my controller handles everything correctly as well.
Ajax-code;
$.ajax({
type: "POST",
url: "http://localhost:8080/NotePath/register",
data: "email=" + email + "&password=" + password + "&name=" + name,
succes: function(response){
$("#formMail, #formPass, #formUsername").val("");
$("#loginRegister, #whiteBg").fadeOut();
},
error: function(xhr,err){
alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
alert("responseText: "+xhr.responseText);
}
});
Controller code;
@RequestMapping(value = "/register")
public @ResponseBody String registerUser(@RequestParam(value = "email") String email, @RequestParam(value ="password") String password,
@RequestParam(value = "name") String name, Model model){
String status = "";
Boolean isEmailTaken = registerServiceImpl.isEmailTaken(email);
Boolean isNameTaken = registerServiceImpl.isNameTaken(name);
if(isEmailTaken == false && isNameTaken == false){
try {
registerServiceImpl.registerUser(email, password, name);
status = "Succes";
} catch (Exception e) {
status = "Error";
e.printStackTrace();
}
}else if(isEmailTaken == true){
status = "mail taken";
}else if(isNameTaken == true){
status = "name taken";
}
return status;
}
I even tried returning a responseEntity;
return new ResponseEntity(status, respHeader, HttpStatus.OK);
When I look at my response header (using google chrome) it even says "mail taken" or "name taken" or whatever message that should appear. But for some reason the ajax success function is not getting triggered...
Weird thing is that I have other ajax calls that do work without any problem.
I thought getting a status code of 200 meant that everything was...well....OK :p
原文:https://stackoverflow.com/questions/21137098
更新时间:2019-05-29 10:29
最满意答案
您没有正确注册回调。
succes: function(response){应该是success: function(response) {
You're not registering the callback correctly.
succes: function(response){ should be success: function(response) {
相关问答
要回答你的具体问题: 我是否可以假设我对这台服务器的每一个成功的请求都会有一个响应代码200(OK)? 答案是肯定的 ,因为TIdHTTPWebBrokerBridge包装了TIdHTTPServer , TIdHTTPServer总是将每个请求的默认响应码设置为200,除非你自己用一个不同的值覆盖它,或者让你的服务器做一些隐式地回复不同的响应代码(比如Redirect()使用302或使用304的SmartServeFile() ),或遇到导致TIdHTTPServer分配4xx或5xx错误响应代
...
您没有正确注册回调。 succes: function(response){应该是success: function(response) { You're not registering the callback correctly. succes: function(response){ should be success: function(response) {
我相信JSON的dataType只是json 。 当数据与预期的数据不匹配时,jQuery还会触发错误处理程序。 例如,将HTML或格式错误的JSON返回到期望JSON的AJAX请求也可以触发错误处理程序,即使响应状态是非错误状态也是如此。 I believe dataType for JSON is simply json. jQuery also fires the error handler when the data doesn't match what is expected. For
...
您的Ajax请求包含以下设置: dataType: "json"
该文档说明了jQuery: 将响应评估为JSON,并返回一个JavaScript对象。 (...)JSON数据以严格的方式解析; 任何格式错误的JSON都被拒绝并抛出一个解析错误。 这意味着如果服务器返回具有200 OK状态的无效JSON,那么jQuery会触发错误函数并将textStatus参数设置为"parsererror" 。 解决方案:确保服务器返回有效的 JSON。 值得注意的是,一个空的响应也被认为是无效的JSON;
...
问题是因为在echo显JSON之前,您在PHP文件中输出了一些HTML。 您可以在JSON response参数中看到它:
↵array…DstagramPost succesfully made.
删除该HTML。 响应文本应该只是这个JSON格式的字符串: {"check":"success"}
The problem is because you have some HTML you are outp
...
success事件在请求成功时被调用( 文档 )。 textStatus参数应包含字符串'200'或服务器返回的任何其他状态编号。 我希望您发布的代码是部分的:我没有看到应该作为参数传递给$.ajax的url,方法等。 The success event is called when the request succeeds (documentation). The textStatus parameter should contain the string '200' or any other
...
我的猜测是,由于您发送的是Access-Control-Allow-Credentials标头,因此无法将*放入Access-Control-Allow-Origin 。 尝试将原点指定为JavaScript客户端的原点。 我基于CORS上的Mozilla开发人员网络文档中的以下信息: origin参数指定可以访问资源的URI。 浏览器必须强制执行此操作。 对于没有凭证的请求,服务器可以将“*”指定为通配符,从而允许任何源访问资源。 My wild guess is that since you
...
可悲的是,这是违约行为。 每当你获得未经授权的访问(401)时,MVC开始并将你重定向到登录页面,你的200(OK)来自哪里,这就是为什么你永远不会得到你想要的响应--401。为了能够返回401代码,你必须明确禁止您的响应的身份验证重定向,如下所示: HttpContext.Response.SuppressFormsAuthenticationRedirect = true;
所以你得到的是: public class AccountController : Controller
{
...
async:false解决了它。 它一直跳到错误功能然后成功 更新1 让它工作的真正原因是因为我在服务器端的web.xml中添加了以下内容:
com.sun.jersey.spi.container.ContainerRequestFilters
com.sun.jersey.api.container.filter.LoggingFilter
...
尝试以下获取状态代码,使用xhr.status作为状态代码: $.ajax({
type: 'POST',
url: 'file.php',
data: { email: $('#andressemail').val() },
success: function(xml, textStatus, xhr) {
alert(xhr.status);
if(xhr.status ==
...
2378




被折叠的 条评论
为什么被折叠?



