jQuery AJAX 成功
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10264929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery AJAX Success
提问by neex1233
I'm using jQuery's $.ajax
function to submit a form, which works, but the success is where I'm having my problem. Here is my code:
我正在使用 jQuery 的$.ajax
函数提交一个表单,该表单有效,但成功是我遇到问题的地方。这是我的代码:
$("#form").submit(function () {
$.ajax({
type: "POST",
url: '/login/spam',
data: formData,
success: function (dataCheck) {
if (dataCheck == 'value') {
//Do stuff
}
}
});
return false;
});
The problem I'm having is the if
function keeps saying that dataCheck doesn't equal value. I know that it does, because when I remove return false;
the page displays value, as expected. Also, I have used an almost identical code before, which works. Could somebody give me some advice?
我遇到的问题是if
函数一直说 dataCheck 不等于value。我知道它确实如此,因为当我删除return false;
页面时,正如预期的那样显示value。另外,我以前使用过几乎相同的代码,它有效。有人能给我一些建议吗?
回答by gdoron is supporting Monica
How to find the answer yourself:
如何自己找到答案:
Place a debug code to see what you get from the server.
放置调试代码以查看您从服务器获得的内容。
$("#form").submit(function () {
$.ajax({
type: "POST",
url: '/login/spam',
data: formData,
success: function (dataCheck) {
console.log(dataCheck); // <==============================
if (dataCheck == 'value') {
//Do stuff
}
}
});
return false;
});
It will probably be in other format than you think.
它的格式可能与您想象的不同。
回答by Shyju
If you want to prevent the default behaviour( in this case the normal form submit), use preventDefaultover return false
; preventDefault will work even if there is a problem in the script which is above return false statement.
如果您想阻止默认行为(在这种情况下是正常表单提交),请使用preventDefaultover return false
;即使在 return false 语句上方的脚本中存在问题, preventDefault 也会起作用。
The below code should work fine.
下面的代码应该可以正常工作。
$("#form").submit(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: '/login/spam',
data: formData,
success: function (dataCheck) {
if (dataCheck == 'value') {
//Do stuff
}
}
});
});
As gdoron mentioned, use console.debug/ alert to see what value is in the variables. Using firebug Net tab / fiddler will help you to understand what response you are getting from the server page.
正如 gdoron 所提到的,使用 console.debug/alert 查看变量中的值。使用 firebug Net tab / fiddler 将帮助您了解从服务器页面获得的响应。
回答by wedgwood
It's strange. My advice is you could use firebug in firefox or something else (e.g. develope tools in chrome) to see the xhr response.
真奇怪。我的建议是你可以在 firefox 中使用 firebug 或其他东西(例如在 chrome 中开发工具)来查看 xhr 响应。
And I think maybe the wrong result cause by wrong type xhr response(e.g. '749afa42e6621f10bae17ee00cb1f4de' envelope with html tag) or some space not trimed.
而且我认为可能是由错误类型的 xhr 响应(例如带有 html 标签的“749afa42e6621f10bae17ee00cb1f4de”信封)或某些未修剪的空间导致的错误结果。
May that help you :)
可能对你有帮助:)
回答by user2698818
I know it might be late however I was having this same issue today and the problem was that the correct string was returned to success, however for some reason it had an extra newline character in front of it. Locally this did not happen however when I put it up online a newline was always added to the string being returned (in your case dataCheck). I used the substr() function to get rid of the newline at the end and it worked fine from there on.
我知道可能会迟到,但是我今天遇到了同样的问题,问题是正确的字符串返回成功,但是由于某种原因,它前面有一个额外的换行符。在本地这并没有发生,但是当我把它放到网上时,总是在返回的字符串中添加一个换行符(在你的情况下是 dataCheck)。我使用 substr() 函数在最后去掉了换行符,从那以后它工作得很好。
回答by Gaurang panchani
if(response.indexOf("success")!=-1){ } Try this method indexOf()
if(response.indexOf("success")!=-1){ } 试试这个方法 indexOf()
回答by Erik Lucio
I guess that dataCheck
is a String. Then use localeCompare()
我猜那dataCheck
是一个String。然后使用localeCompare()
if (dataCheck.localeCompare('value') == 0) {
//Do stuff
}
- Returns -1if str1 is sorted before
str2
- Returns 0if the two strings are equal
- Returns 1if
str1
is sorted afterstr2
- 如果 str1 在之前排序,则返回-1
str2
- 如果两个字符串相等则返回0
- 如果排序后返回1
str1
str2