0

I am making a POST to the server, which returns an object.

The object is JSON and looks like this;

Object {yourVote: 7, totalVotes: 41, average: "6.9"} 

I want to get the text and values from this object and use them in my html - give them classes, color, etc.

How would i go about achieving that?

So far, i have tried fetching the text of the object using .txt() but that returns an error of undefined. I have also tried converting that JSON object to an array so i can access its values that way but, alas, that returns an error again.

Here is my code:

My AJAX request:

 $.ajax({
            dataType: "JSON",
            type: "GET",
            url: "/api/phometervote",
            data: {
                articleId: articleId,
                vote: userVote
            },
            success: function (rn) {

                var message = rn,
                    messageContent = message.makeArray();

                // I would like to be able to access the object as an array
                //So that way i can access each item individually
                //This returns an error
                $(".someClass").html(messageContent[0]);

            }

        });

1 Answer 1

3

You can access each field using the response object, try this way:

  $.ajax({
        dataType: "JSON",
        type: "GET",
        url: "/api/phometervote",
        data: {
            articleId: articleId,
            vote: userVote
        },
        success: function (response) {

            $(".someClass").html(response.yourVote);
            $(".someClass1").html(response.totalVotes);
            $(".someClass2").html(response.average);

        }

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

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.