0

I know this has been answered to some degree elsewhere but nothing I tried seemed to do the trick. I have never used jQuery for this (I always had something like Angular's $http to handle this) but I'm getting an Object Object where it should spit out the email info. I've included the code below.

    var email = $("#email").val();
    $.post("http://httpbin.org/post", email, function(email, status){
        console.log("Email: " + email + "\nStatus: " + status);
    });

What am I doing wrong?

2
  • console.log(email) Commented Apr 5, 2017 at 1:42
  • Try function(data){ console.log('Email:' + data.email)} as your success method. Commented Apr 5, 2017 at 1:57

5 Answers 5

1

You probably want to try using JSON.stringify() for your object as this returns the string representation which is what you want

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

Comments

1

Thanks to you all. It helped me arrive at the answer. Which is:

      $.post("http://httpbin.org/post", { email: email } , function(data, status){
        console.log(data.form.email);
      });

Comments

0

You can replace plus + by comma ,!

var email = $("#email").val(); $.post("http://httpbin.org/post", email, function(email, status){ console.log("Email: ", email, "Status: ", status); });

hope this can help you!

Comments

0

The syntax of $.post() is:

$.post( "ajax/test.html", function( response) {
    // here response contains the server response in it
});

In your case, if it is showing Object Object, means the response is a JSON object, so you have to parse it like:

$.post( "ajax/test.html", function( response) {
    var data = JSON.parse(response);
    console.log(data.email);
    console.log(data.status);
});

Comments

0

$.post is the shorthand of the $.ajax

$.ajax({
     type: "POST",
     url: url,
     data: data,
     success: success,
     dataType: dataType
});

If you add console.log(email), it will display the content in the object email

var email = $("#email").val();
$.post("https://httpbin.org/post", email, function(email, status){
    console.log("Email: " + email + "\nStatus: " + status);
    console.log(email);
});

enter image description here

https://api.jquery.com/jquery.post/

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.