[英]how to get the encoded data by json_encode in the java script file
我正在使用json_encode
對文件數據進行編碼,這是我使用Javascript發送編碼后的數據時得到的:
這是我的php代碼(使用codeigniter上傳文件):
$file_info = $this->upload->data();
echo json_encode($file_info);
我使用我的javascript文件中的數據:
'onUploadSuccess' : function(file, data, response) {
alert('The file was saved to: ' + data);
}
如何使用file_name或其他字符串進行編碼?
例如我如何使用:
'onUploadSuccess' : function(file, data, response) {
alert('The file name is: ' + file_name);
}
您的響應中有3個變量。 要查看執行代碼時每個代碼是什么,請使用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);
}
現在打開您的JavaScript日志(Chrome中為F12,Firefox中為Shift + F5)。 Json數據應該已經轉換為對象。 如果格式為json,請添加JSON.parse(data)
。
對象是javascript的骨干。 要在名為data
的對象中選擇信息,請使用data.property
。 因此data.file_name
應該返回“ 83274983279843.jpg”, data.type
將返回類型, data.type
。
編輯:所以在聊天中討論之后,問題是您沒有解析JSON。 我也錯誤地告訴您反轉變量順序。
這是固定代碼:
'onUploadSuccess' : function(file, data, response) {
data = JSON.parse(data)
alert('The file : ' + data.file_type);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.