1

This is my JS code

function process_file(file_name) {
    $.ajax({
        type: "POST",
        url: "process_file.php?file_name="+file_name,
        datatype : "json",
        cache: true,
        success: function(data) {
            console.log(data);
            //alert(data);

            var error_flag = data[0]["error_flag"];
            var database_insert = data[0]["database_insert"];
            var mysql_message = data[0]["mysql_message"];
            var excel_read_message = data[0]["excel_read_message"];

            alert(mysql_message);

            $("#error_flag").html(error_flag);
            $("#database_insert").html(database_insert);
            $("#mysql_message").html(mysql_message);
            $("#excel_read_message").html(excel_read_message);

        }
    });
}

Console log that is displayed:

[{"error_flag":true,"database_insert":true,"mysql_message":"Data Inserted Successfully","excel_read_message":null}]

I want to extract each variable in js code. I have tried various but not getting what is desired.

  • What exactly is the problem? Do you have an error? Which like doesn't work? At a glance, this code seems valid... – Martin Tournoij Jul 21 '14 at 06:50
  • Alert is not producing any data its always blank. –  Jul 21 '14 at 06:51
  • Have you tried to Google your problem? First hit: http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript – Bart Friederichs Jul 21 '14 at 06:51
  • 1
    Do you mean that you cannot access the variable? I think it is `dataType`, not a `datatype`. if `dataType:'json'` will give the javascript object in success callback instead of string. – Fizer Khan Jul 21 '14 at 06:51
  • Your response is in `json` format as an array **`[]`** with an object **`{}`**. Look at Orion's answer below for the correct notation to achieve what you want. – Darren Jul 21 '14 at 06:52
  • 2
    Thanks @FizerKhan it was a typo. How did I not check that! Thanks any ways for highlighting it. –  Jul 21 '14 at 06:54

2 Answers2

1

I think, the data comes as a string, thats why you cannot access the members. Change datatype to dataType

function process_file(file_name) {
    $.ajax({
        type: "POST",
        url: "process_file.php?file_name="+file_name,
        dataType : "json",
        cache: true,
        success: function(data) {
            console.log(data);
            //alert(data);

            var error_flag = data[0]["error_flag"];
            var database_insert = data[0]["database_insert"];
            var mysql_message = data[0]["mysql_message"];
            var excel_read_message = data[0]["excel_read_message"];

            alert(mysql_message);

            $("#error_flag").html(error_flag);
            $("#database_insert").html(database_insert);
            $("#mysql_message").html(mysql_message);
            $("#excel_read_message").html(excel_read_message);

        }
    });
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
0

Problem here is the way you are accessing json attribute is not correct. as your json under curley bracket is not array.

You'll have to do like below:

var error_flag = data[0].error_flag;
// repeat it for other vars
Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62