在Ajax Jquery函数中添加if else函数
是否有可能在我的JS中添加其他function:
如果响应==成功重定向到家
如果响应==失败重定向失败
$.ajax({ type: "POST", url: action, data: form_data, success: function(response) { if (response == 'success') window.location.replace("home"); else $("#message").html("Invalid username and/or password.
"); } });
success: function(response) { if (response == 'success') window.location.replace("home"); else if (response == "failed") window.location.replace("failed"); else $("#message").html("Invalid username and/or password.
"); }
或者你的意思是:
$.ajax({ type: "POST", url: action, data: form_data, error: function() { window.location.replace("Failed"); }, success: function(response) { if (response == 'success') window.location.replace("home"); else $("#message").html("Invalid username and/or password.
"); } });
用这个:
$.ajax({ type: "POST", url: action, data: form_data, success: function(response) { if (response == 'success') window.location.replace("home"); else if (response == 'failure') { $("#message").html("Invalid username and/or password.
"); window.location.replace("failed"); } } });
你也可以这样做
$.ajax( "example.php" ) .done(function(msg) { alert("success"); if(msg == "success") ... else ... }) .fail(function() { alert("error"); });
请记住,这仅适用于> jQuery-1.8
- 如何对jQuery Promises进行unit testing?
- 在IE中,JQuery没有正确地解析attr(“href”)
- MVC5 – 如何使用jquery ajax将fileupload与模型一起传递给控制器
- 如何在使用jQuery克隆表单元素后调用AJAX函数?
- net.sf.json.JSONException:Struts 2中的层次结构中有一个循环
- jQuery:如何获取动态生成元素的ID?
- 为什么Ajax在第一次请求时正常工作,但在第二次请求时返回新页面上的部分视图?
- IOS6和Safari Photo Uploading – 文件API + Canvas + jQuery Ajax异步上传和调整文件大小
- Firebase REST API POST请求失败,并显示错误:“数据无效; 无法解析JSON对象,数组或值…“