javascript - Ajax 响应中的 JQuery 无法识别的表达式

标签 javascript jquery ajax

我有这个 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);
            }
        });
    })

在响应上,当我 console.log(data);在控制台中,它向我显示了响应的html,但是当我这样做时console.log($(data));它给了我这个错误

未捕获错误:语法错误,无法识别的表达式:

<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

我就是这么做的

  $(data) 

因为我想获取响应的 HTML 页面的片段

最佳答案

自从我升级到 jQuery 1.9.x 以来,我遇到了完全相同的问题。实际上,简单的 $(data) 会导致崩溃,仅此而已。我之前遇到过几次这个问题(没有1.9之前的版本),但我不记得它的问题...

无论如何,这是一个完全阻塞的麻烦......仍在寻找原因和解决方案。

编辑:

如果我这样做:

$.ajax('/',function(html){
  html = $('<div></div>').append(html);
  $(html);
});

效果很好。 如果我这样做:

$.ajax('/',function(html){
  $(html);
});

它给了我一个(通过 FF 错误控制台):

Erreur : Error: Syntax error, unrecognized expression: <div id="...">(...)
jquery.min.js - line : 4

编辑2:

好的找到了解决方案...经过长时间的搜索和测试: http://stage.jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring

所以我现在做类似的事情:

$.ajax('/',function(html){
  $($.parseHTML(html));
});

无论如何,jQuery 总是将我的第一个元素与第一个元素混淆,但至少它可以工作

关于javascript - Ajax 响应中的 JQuery 无法识别的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14667441/

相关文章:

javascript - 如何使用javascript从子元素中获取值

javascript - jQuery 返回选定选项属性值

javascript - JS : Automatic scroll when mouse idle, 手指未触摸?

javascript - 将数据从可编辑表单保存到数据库

javascript - location.href 在 ajax 之后不起作用(更多详细信息)

javascript - 从 5 个组合框中获取选定的值

javascript - 我怎样才能对完全间隔的名称进行模式匹配?

javascript - 使用javascript预加载图像并在准备好时显示

javascript - 当 css 媒体屏幕查询启动时,是否有要捕获的浏览器事件?

javascript - 通过基于选择的选择选项获取动态添加 DIV