12

i have a validation.js file

var name = $("#name");

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

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

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

test.jsp

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

the login user.jsp file

<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

2
  • What happens what you do console.log(msg)? What does that object look like? Commented Feb 7, 2011 at 17:30
  • 8
    [object Object] is not an error, its the string representation of a basic javascript object. Which means your server is probably printing a JSON or an object of some kind. Commented Feb 7, 2011 at 17:35

4 Answers 4

12

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]"

Sign up to request clarification or add additional context in comments.

Comments

5

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

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

2 Comments

thks...the error is removed but the output is coming is next line below text box,i want it to appear in the same line besides the name textbox
I also want to compare the msg value in function(msg) value with a string
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.