0

i have this json in my request

    $.get("/shop_possystems/index.php?route=module/cart/ajax_get_individual_prices&current_id=" + current_id + "&standard_id=" + standard + "&professional_id=" + professional + "&premium_id=" + premium + "&quantity=" + quantity,
        function(data) {
            var standard_price     = data.standard_price;
            var professional_price = data.professional_price;
            var premium_price      = data.premium_price;
                        console.log(data);
                    $prettyCheckBox0.text(standard_price);
                    $prettyCheckBox1.text(professional_price);
                    $prettyCheckBox2.text(premium_price);
    });

the problem is that the data.standard_price returns undefined but in the console.log i have this

 "{"standard_price":"included","professional_price":"add $792.00","premium_price":"add $3372.00"}"

whats the deal

4 Answers 4

5

You need to parse the JSON. Put this as the first line of the callback:

data = $.parseJSON(data);

Alternatively, use $.getJSON instead of $.get or pass "json" as another parameter to $.get.

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

Comments

2

You should use $.parseJSON(string), like this:

 $.get("/shop_possystems/index.php?route=module/cart/ajax_get_individual_prices&current_id=" + current_id + "&standard_id=" + standard + "&professional_id=" + professional + "&premium_id=" + premium + "&quantity=" + quantity,
        function(data) {data = $.parseJSON(data);
            var standard_price     = data.standard_price;
            var professional_price = data.professional_price;
            var premium_price      = data.premium_price;
                        console.log(data);
                    $prettyCheckBox0.text(standard_price);
                    $prettyCheckBox1.text(professional_price);
                    $prettyCheckBox2.text(premium_price);
    });

Another way is to use $.ajax an specify in dataType: 'json' see http://api.jquery.com/jQuery.ajax/

Comments

1

Try using jQuery.getJSON()

http://api.jquery.com/jQuery.getJSON/

And when returning your data from the php script, use json_encode()

Comments

1

console.log seems to be indicating that you have a string, rather than an actual Javascript object. You need to tell jQuery to treat the content as JSON and to give you the parsed object. The easiest way is to use $.getJSON instead of $.get:

$.getJSON("/shop_possystems/index.php?route=module/cart/ajax_get_individual_prices&current_id=" + current_id + "&standard_id=" + standard + "&professional_id=" + professional + "&premium_id=" + premium + "&quantity=" + quantity,
    function(data) {
        var standard_price     = data.standard_price;
        var professional_price = data.professional_price;
        var premium_price      = data.premium_price;
                    console.log(data);
                $prettyCheckBox0.text(standard_price);
                $prettyCheckBox1.text(professional_price);
                $prettyCheckBox2.text(premium_price);
});

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.