javascript - how to get the encoded data by json_encode in the java script file -
i encoding file data using json_encode
, when send encoded data in javascript:
this php code (using codeigniter upload file):
$file_info = $this->upload->data(); echo json_encode($file_info);
and use data in javascript file:
'onuploadsuccess' : function(file, data, response) { alert('the file saved to: ' + data); }
how can use file_name or other strings encoded?!
for example how can use:
'onuploadsuccess' : function(file, data, response) { alert('the file name is: ' + file_name); }
you have 3 variables in response. see each 1 when code executed, use console.log().
'onuploadsuccess' : function(file, data, response) { console.log('\n"data" variable:') console.log(data); console.log('"file" variable:') console.log(file); console.log('\n"response" variable:') console.log(response); }
now open javascript log (f12 in chrome, shift+f5 think in firefox). json data should have been converted object. if it's in json form, add json.parse(data)
.
objects backbone of javascript. select information in object named data
, use data.property
. data.file_name
should return "83274983279843.jpg", data.type
return type, etc.
edit: after discussion in chat issue didn't parse json. incorrectly told reverse variable order.
here fixed code:
'onuploadsuccess' : function(file, data, response) { data = json.parse(data) alert('the file : ' + data.file_type); }
Comments
Post a Comment