jquery:-[object Object] error

后端 未结 4 983
难免孤独
难免孤独 2021-01-11 10:44

i have a validation.js file

var name = $(\"#name\");

    $.ajax({
        type:       \"get\",
        url:        \"test.jsp\",
        data:           \"n         


        
相关标签:
4条回答
  • 2021-01-11 11:22

    msg appears 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());

    0 讨论(0)
  • 2021-01-11 11:26

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

    var name = $("#name").val();
    var name = $("#name").text(); 
    
    0 讨论(0)
  • 2021-01-11 11:33

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

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

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

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

    <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.

    0 讨论(0)
  • 2021-01-11 11:39

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

    The reason you see [object Object] is because msg is of type object and you pass it into .html which 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]"

    0 讨论(0)
提交回复
热议问题