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.

asked Jul 21, 2014 at 6:47
6
  • What exactly is the problem? Do you have an error? Which like doesn't work? At a glance, this code seems valid... Commented Jul 21, 2014 at 6:50
  • Alert is not producing any data its always blank. Commented Jul 21, 2014 at 6:51
  • Have you tried to Google your problem? First hit: stackoverflow.com/questions/4935632/… Commented Jul 21, 2014 at 6: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. Commented Jul 21, 2014 at 6:51
  • 2
    Thanks @FizerKhan it was a typo. How did I not check that! Thanks any ways for highlighting it. Commented Jul 21, 2014 at 6:54

2 Answers 2

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);
 }
 });
answered Jul 21, 2014 at 6:56
Sign up to request clarification or add additional context in comments.

Comments

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
answered Jul 21, 2014 at 6:50

3 Comments

No, we can access the members inside object by both ways data[0].error_flag; and data[0]["error_flag"];
Thanks for answer but updating dataType solved the problem.
@FizerKhan - Thanks a lot for suggesting another alternative; was unaware of that.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.