Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am trying to display data that received from jquery post. but it is giving me an error saying undefined

my php json encoded array is this.

[{"matId":"7","matName":"test","matBrand":"est","matPackaging":"1","matWidth":"434","matHeight":"23","matLength":"23","matWeight":"23","matArea":"23","matVolume":"23","matPerPack":"32","supplier1":"19","supplier2":"19","supplier3":"19","requiredInPhase":"","stockItem":"1"}]

then in jquery I am trying to alert one of this value, but it gives error 'undefined'.

this is my jquery code

$('#matId').on('change', function() {
            var matId = $(this).val();
            $.post('<?php echo base_url() . 'display_mat_details'; ?>', {matId: matId}, function(redata) {
                var obj = $.parseJSON(redata);
                alert(obj.matId);
            });
        });
share|improve this question

4 Answers 4

up vote 1 down vote accepted

Your data is enclosed within the [] that's an array.So u need to use [0] to get first object from the array

$('#matId').on('change', function() {
  var matId = $(this).val();
  $.post('<?php echo base_url() . '
    display_mat_details '; ?>', {
      matId: matId
    }, function(redata) {
      var obj = $.parseJSON(redata);
      alert(obj[0].matId);
    });
});

Example:

var str = [{"matId":"7","matName":"test","matBrand":"est","matPackaging":"1","matWidth":"434","matHeight":"23","matLength":"23","matWeight":"23","matArea":"23","matVolume":"23","matPerPack":"32","supplier1":"19","supplier2":"19","supplier3":"19","requiredInPhase":"","stockItem":"1"}];
alert(str[0].matId);

share|improve this answer
    
This does work, however doesn't look overly cool. If the OP has access to change the returned data then that would be the first choice rather than subscripting the result. –  Nilotpal Barpujari Nov 13 '14 at 12:36

Thats because your JSON is an array containing an object... Remove the square brackets around the JSON.

share|improve this answer

and to use

$.parseJSON()

your array structure should be like this

var Content = '{"matId":"7","matName":"test","matBrand":"est","matPackaging":"1","matWidth":"434","matHeight":"23","matLength":"23","matWeight":"23","matArea":"23","matVolume":"23","matPerPack":"32","supplier1":"19","supplier2":"19","supplier3":"19","requiredInPhase":"","stockItem":"1"}';

var obj = $.parseJSON(Content);
alert(obj.matId);
share|improve this answer

Use this code replace obj.matId with obj[0].matId

  $('#matId').on('change', function() {
                var matId = $(this).val();
                $.post('<?php echo base_url() . 'display_mat_details'; ?>', {matId: matId}, function(redata) {
                   // var obj = $.parseJSON(redata);// error here structure of redata is wrong
                   // alert(obj[0].matId);
                    alert(redata[0].matId);
                });
            });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.