Laracast13's avatar

retrieve data from JSON file using jQuery and Ajax

Hi

Ajax gives data and in console log it look like: {priceType: [{pricea: 50, priceb: 20}]}


        $('#list').on('change', function(){ 
            var list = $("#list").val();
            if(list != "") {
                $.ajax({
                    url: "{{  url('/type/price/') }}/"+list,
                    type:"POST",
                    dataType:"json",
                    success:function(data) {
                      if(data){ 
                          $("#type_price_a").val(priceType.pricea);  
                          $("#type_price_b").val(priceType.priceb);       
                      }
                    },
                });
            } 
        });

I want put it in value, but i can not get data using priceType.pricea and priceType.priceb

0 likes
5 replies
tykus's avatar
tykus
Best Answer
Level 104

The information will be inside data, so

success:function(data) {
	if(data){ 
		$("#type_price_a").val(data.priceType.pricea);  
		$("#type_price_b").val(data.priceType.priceb);       
	}
},
tykus's avatar

Ajax gives data and in console log it look like: {priceType: [{pricea: 50, priceb: 20}]}

Is this still the case?

tykus's avatar

I just realised that priceType is an array with one object inside:

success:function(data) {
	if(data){ 
		$("#type_price_a").val(data.priceType[0].pricea);  
		$("#type_price_b").val(data.priceType[0].priceb);       
	}
},
1 like

Please or to participate in this conversation.