1

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);
            });
        });
0

4 Answers 4

1

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);

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

1 Comment

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.
1

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

Comments

1

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);

Comments

1

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);
                });
            });

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.