在jquery中成功地将文件传输到服务器(Cordova文件传输)时,我得到object作为响应。
现在对象中有数组,如下所示:
Array
(
[file] => Array
(
[name] => socialnoise51@gmail.com1437742503025
[type] => image/jpeg
[tmp_name] => /tmp/php4TR9pw
[error] => 0
[size] => 348392
)
)
如何获取name的值?
发布于 2015-07-24 21:29:45
显示的代码看起来像是php数组的打印。这不是可在ajax请求中读取的有效响应。使用json_encode()
回显数组的json表示
echo json_encode($data);
我不熟悉cordova文件传输,所以你可能需要也可能不需要使用JSON.parse(results)
解析响应以转换为javascript对象。
如果此传输使用$.ajax
执行此操作,则将dataType设置为'json'
将在内部执行解析
发布于 2015-07-24 21:45:04
就像charlietfl已经说过的,在设备和服务器之间处理数据的一种正常方式是jQuery提供的ajax。用于从服务器检索数据的Ajax函数将如下所示:
function getDataFromServer() {
var term=null;
$.ajax({
url:'http://yourUrlGoesHere.tld',
type:'GET',
data:term,
dataType:'json', //defines what you get back from the server
error:function(jqXHR,text_status,strError){},
success:function(data) //{
for (i=0; i < data.items.length; i++){
//This is the part, where you can do what you want with anything inside the array.
console.log(data); //i would simply do this to first see the result inside the console
}
}
})
}
所以,属于你的注释,你应该把你的数组保存在一些东西里面。例如,名为myArray。现在可以通过myArray.shorturl
访问shorturl
了。
试试像console.log(myArray.shorturl);
这样的东西
https://stackoverflow.com/questions/31611558
复制相似问题