[英]how to get the encoded data by json_encode in the java script file
I am encoding file data using json_encode
and this is what I get when I send the encoded data in Javascript: 我正在使用
json_encode
对文件数据进行编码,这是我使用Javascript发送编码后的数据时得到的:
This is my php code (using codeigniter to upload a file): 这是我的php代码(使用codeigniter上传文件):
$file_info = $this->upload->data();
echo json_encode($file_info);
and I use the data in my javascript file: 我使用我的javascript文件中的数据:
'onUploadSuccess' : function(file, data, response) {
alert('The file was saved to: ' + data);
}
How can I use file_name or other strings as they are encoded?! 如何使用file_name或其他字符串进行编码?
for example how can I use: 例如我如何使用:
'onUploadSuccess' : function(file, data, response) {
alert('The file name is: ' + file_name);
}
You have 3 variables in your response. 您的响应中有3个变量。 To see what each one is when your code is executed, use console.log().
要查看执行代码时每个代码是什么,请使用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 up your javascript log (F12 in Chrome, Shift+F5 I think in firefox). 现在打开您的JavaScript日志(Chrome中为F12,Firefox中为Shift + F5)。 The Json data should have been converted into an object.
Json数据应该已经转换为对象。 If it's in its json form, add
JSON.parse(data)
. 如果格式为json,请添加
JSON.parse(data)
。
Objects are the backbone of javascript. 对象是javascript的骨干。 To select information in an object named
data
, you use data.property
. 要在名为
data
的对象中选择信息,请使用data.property
。 So data.file_name
should return "83274983279843.jpg", data.type
would return the type, etc. 因此
data.file_name
应该返回“ 83274983279843.jpg”, data.type
将返回类型, data.type
。
Edit: So after discussion in chat the issue was you didn't parse the JSON. 编辑:所以在聊天中讨论之后,问题是您没有解析JSON。 Also I incorrectly told you to reverse the variable order.
我也错误地告诉您反转变量顺序。
Here is the fixed code: 这是固定代码:
'onUploadSuccess' : function(file, data, response) {
data = JSON.parse(data)
alert('The file : ' + data.file_type);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.