1

Seems like this should be easy!

I am submitting a form via jQuery/ajax and the success output is a JSON sting like this:

{"activities-tracker-steps":[{"dateTime":"2016-09-06","value":"0"}]}

All I want to do is get the value (in this case 0). So, I try this:

        submitHandler: function() {
        var postData = $("#form").serializeArray();
        var formURL = $("#form").attr("action");
        $.ajax({
            url : formURL,
            type: "POST",
            data : postData,
            success : function(data) {
                var json;
                json = $.parseJSON(data);
                alert(json['value']);
                $('#form').trigger("reset");            
                $("#success").show().fadeOut(3000);
            },
            error : function() {
                $("#fail").show().fadeOut(3000);
            }
        });
    }   

Undefined, apparently. What am I doing wrong!?

Many thanks for your thoughts,

1 Answer 1

1

As per provided structure you can fetch value by json['activities-tracker-steps'][0].value as mentioned below.

submitHandler: function() {
        var postData = $("#form").serializeArray();
        var formURL = $("#form").attr("action");
        $.ajax({
            url : formURL,
            type: "POST",
            data : postData,
            success : function(data) {
                var json;
                json = $.parseJSON(data);
                alert(json['activities-tracker-steps'][0].value);
                $('#form').trigger("reset");            
                $("#success").show().fadeOut(3000);
            },
            error : function() {
                $("#fail").show().fadeOut(3000);
            }
        });
    }   
Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant, thanks! Does [0] represent the first layer of an array then?
value is property of that object which is in array, you can console.log(json) after parse json. and check in inspect view of browser.... If you like and work then dont forget to mark as correct and upvote it.

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.