Javascript jquery:-[object 对象] 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4924383/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:47:57  来源:igfitidea点击:

jquery:-[object Object] error

javascriptjquery

提问by Prerna

i have a validation.js file

我有一个validation.js 文件

var name = $("#name");

    $.ajax({
        type:       "get",
        url:        "test.jsp",
        data:           "name="+name,
        success:    function(msg) {

            $('#result').hide();

            $("#result").html(msg)
            .fadeIn("slow");
        }
    });

test.jsp

测试.jsp

</head>
    <body>
        <h1><%
        String user=request.getParameter("name");
        out.print(user);
        %></h1>
    </body>

the login user.jsp file

登录 user.jsp 文件

<form method="post" id="customForm" action="welcome.html">
        <div>
            <label for="name">Name</label>
            <input id="name" name="name" type="text" />
                            <span id="nameimage"></span>

            <span id="nameInfo"></span>
                          <p id="result"></p>  
        </div>

i have to show the username in my form as soon as user goes to next feild in my form.but it is showing [object Object] error in place where p tag starts

一旦用户转到表单中的下一个字段,我就必须在表单中显示用户名。但是它在 p 标记开始的位置显示 [object Object] 错误

回答by Raynos

looks like msgisn't what you expect. I think you want msg.responseText

看起来msg不是你所期望的。我想你想要msg.responseText

The reason you see [object Object] is because msgis of type object and you pass it into .htmlwhich will convert it to a string. And thus the html is filled with the string representation of object which in this case is "[object Object]"

您看到 [object Object] 的原因是因为它msg是 object 类型,您将其传递给.html它将转换为字符串。因此,html 填充了对象的字符串表示,在这种情况下是“[object Object]”

回答by a'r

You will need to pass the value or the text of the #name object. Like this:

您将需要传递 #name 对象的值或文本。像这样:

var name = $("#name").val();
var name = $("#name").text(); 

回答by lonesomeday

msgappears to be a document object, rather than a string containing the appropriate name. It seems to me that you want $('#response').text($(msg).find('h1').text());

msg似乎是一个文档对象,而不是包含适当名称的字符串。在我看来你想要$('#response').text($(msg).find('h1').text());

回答by demula

The good-old IE expects to redirect somewhere when you use an anchor tag. If you have something like the following:

当您使用锚标记时,古老的 IE 期望重定向到某个地方。如果您有类似以下内容:

<a href="javascript: submit()">Submit</a>

IE will show you blank page with [Object object]when using JSON, even if submit()uses ajax.

IE 会[Object object]在使用 JSON 时显示空白页面,即使submit()使用 ajax。

You can use onClick instead or javascript:void sumbit()like this:

您可以改用 onClick 或javascript:void sumbit()像这样:

<a id="btn-submit">Submit</a>
<script>
    $(document).on('click', '#btn-submit', function(event){
        event.preventDefault();
        submit();
    });
</script>

I didn't test the void solution but a coworker of mine says that's the one he uses.

我没有测试 void 解决方案,但我的一位同事说这是他使用的解决方案。