Javascript JQuery加载页面片段会返回一个未定义的

Javascript JQuery加载页面片段会返回一个未定义的,javascript,jquery,ajax,jsp,twitter-bootstrap,Javascript,Jquery,Ajax,Jsp,Twitter Bootstrap,下面是一个由ajax返回的页面 <!DOCTYPE HTML> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>Insert title here</title> <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script> </head>

下面是一个由ajax返回的页面

<!DOCTYPE HTML>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
</head>
<body>

    <div id = "div1">
        <h1>HI am a dashboard</h1>
        <h1>HI am a dashboard</h1>
        <h1>HI am a dashboard</h1>
    </div>
</body>
</html>
console.log($(data.find('#div1').html())
没有定义。而

console.log(data);
返回我之前声明的页面

更新 发现问题后,我的代码中有两个“数据”变量。我成功地更改了一个,但它仍然返回未定义

$('#submit-login').on('click',function(e){
        $('#hidden-submit-login').click();
})

    $('body').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(response) {
                console.log($(response).find('#div1').html());
                console.log(response);
                //console.log($((html).find('#div1').html();
            }
        });
    });

您正在对
字符串
而不是DOM元素应用jQuery选择器。
响应
对象是请求的结果;它不存在于DOM中

就像
document.getElementById('div1')
将返回
null

在使用DOM之前,应该创建/附加一个HTML元素到DOM中

如果您要做的是解析发出请求的页面内的
块:

首先,我建议您去掉所有其他标记(包括
中的所有内容)。因此,您的文件仅包括:

<div id="div1">
    <h1>HI am a dashboard</h1>
    <h1>HI am a dashboard</h1>
    <h1>HI am a dashboard</h1>
</div>
现在将
div1
附加在页面正文的底部,这样就可以访问它了

console.log($('#div1').html());

注意:请确保id
div1
是唯一的,以便在执行进一步操作时不选择其他现有元素。

这有帮助吗?已经尝试过了,但仍然不起作用。您需要查看完整的ajax代码。您能创建一个JSFIDLE吗?我该怎么做?如何将响应对象转换为DOM?或者我怎样才能得到第一名?
$.ajax({
    url : url,
    type : "POST",
    data : data,
    dataType: "text",
    cache : false,
    success : function(data) {
        if (data) {
            $('body').append(data); //this will append the html inside the response; at the end bottom of your page's body
            console.log($('#div1').html());
        }
    }
});
console.log($('#div1').html());