javascript Ajax 响应上的 JQuery 无法识别的表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14667441/
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 unrecognized expression on Ajax response
提问by user962206
I have this JQuery Ajax Form.
我有这个 JQuery Ajax 表单。
$('#modal-body-sign-in').on('submit', '#sign-in', function(e) {
e.preventDefault();
var data = $(this).serialize();
var url = $(this).attr('action');
$.ajax({
//this is the php file that processes the data and send mail
url : url,
type : "POST",
data : data,
dataType: "html",
//Do not cache the page
cache : false,
//success
success : function(data) {
//console.log(data.content);
console.log($(data));
//$('#modal-body-sign-in').html(data);
}
});
})
on the response, when i console.log(data); in the console it shows me the responded html
but when I do this console.log($(data));
it gives me this error
在响应中,当我console.log(data); in the console it shows me the responded html
但当我这样做时,console.log($(data));
它给了我这个错误
Uncaught Error: Syntax error, unrecognized expression:
未捕获的错误:语法错误,无法识别的表达式:
<html>
<head>
<style>
ul {
margin: 0%;
}
ul li {
display: inline;
list-style-type: none;
float: left;
}
</style>
<title>Insert title here</title>
</head>
<body>
<!-- Ajax Container. DO NOT REMOVE THE DIV CONTENT FOR AJAX ACCESS! -->
<div id="content">
<div id="modal-body">
<form id="doLogin" name="doLogin" action="/HitPlay/doLogin" method="post">
<fieldset>
<ul id="doLogin_" class="errorMessage">
<li><span>Incorrect username or password</span></li> </ul>
<br/>
<br/>
<label>Username</label>
<input type="text" name="userBean.username" value="" id="doLogin_userBean_username"/>
<br/>
<label>Password</label>
<input type="password" name="userBean.password" id="doLogin_userBean_password"/>
<br />
<input type="submit" id="doLogin_0" value="Submit"/>
</fieldset>
</form>
</div>
</div>
</body>
</html> jquery-1.9.0.min.js:2
a.error jquery-1.9.0.min.js:2
f jquery-1.9.0.min.js:2
x jquery-1.9.0.min.js:2
a jquery-1.9.0.min.js:2
st.fn.extend.find jquery-1.9.0.min.js:2
st.fn.st.init jquery-1.9.0.min.js:1
st jquery-1.9.0.min.js:1
$.ajax.success localhost:59
f jquery-1.9.0.min.js:1
p.fireWith jquery-1.9.0.min.js:1
r jquery-1.9.0.min.js:3
r
I was doing this
我在做这个
$(data)
because I want to get a fragment of the responded HTML page
因为我想获得响应的 HTML 页面的片段
回答by Baptiste LARVOL-SIMON
I've got exactly the same problem since I've upgraded to jQuery 1.9.x. actually the simple $(data) generates the crash, nothing else. I had this problem a few times before (without previous versions than 1.9), but I don't remember its issue...
自从我升级到 jQuery 1.9.x 以来,我遇到了完全相同的问题。实际上,简单的 $(data) 会导致崩溃,仅此而已。我以前遇到过这个问题几次(没有比 1.9 之前的版本),但我不记得它的问题......
anyway it is a totally blocking trouble... still looking for a reason for that and a fix.
无论如何,这是一个完全阻塞的问题......仍在寻找原因和解决方案。
EDIT:
编辑:
If I'm doing that :
如果我这样做:
$.ajax('/',function(html){
html = $('<div></div>').append(html);
$(html);
});
It works fine. If I do :
它工作正常。如果我做 :
$.ajax('/',function(html){
$(html);
});
It gives me a (through the FF error console) :
它给了我一个(通过 FF 错误控制台):
Erreur : Error: Syntax error, unrecognized expression: <div id="...">(...)
jquery.min.js - line : 4
EDIT 2:
编辑2:
Ok found the solution... after a long search and tests : http://stage.jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring
好的找到了解决方案......经过长时间的搜索和测试:http: //stage.jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring
So I now do something like :
所以我现在做类似的事情:
$.ajax('/',function(html){
$($.parseHTML(html));
});
Anyway jQuery is always confusing my first element with the one, but at least it works
无论如何,jQuery 总是将我的第一个元素与第一个元素混淆,但至少它有效
回答by Ryan Ore
If you are using jQuery1.9, the problem may lie in the content being loaded. There is a new update which requires that the first character in the response be a <[AKA the less than symbol]. Even whitespace will cause this to break and throw the dreaded "Uncaught Error: Syntax error, unrecognized expression:" error.
如果您使用的是 jQuery1.9,问题可能在于正在加载的内容。有一个新的更新要求响应中的第一个字符是<[AKA小于符号]。即使是空格也会导致它中断并抛出可怕的“未捕获的错误:语法错误,无法识别的表达式:”错误。
I'd recommend checking this before using the suggested workaround above. Its not a bug its a security effort.
我建议在使用上面建议的解决方法之前先检查一下。它不是一个错误,而是一项安全工作。
http://jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring
http://jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring