这就是我目前正在做的事情。我尝试将值从 my 推json
入 links 数组,但在输入数组后,值变为undefined
. 移动数据并使其仍然可用的更好方法是什么?
var links = [];
$.ajax({
url: 'data.json',
dataType: 'json',
type: 'get',
cache: false,
success: function(data) {
$(data.emails).each(function(index, value) {
links.push(value);
});
}
});
//data.json contains:
{
"emails": [{
"source": "[email protected]",
"target": "[email protected]"
}]
}
使用此代码的结果是相同的。值进入数组,但是当您通过链接访问它们时。(值的名称)它返回“未定义”
$.each(data.emails, function(index, value) {
links.push(value);
console.log(links);
return value;
});
试试这个
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script type="text/javascript">
//alert("data");
var links = [];
$.ajax({
url: 'test1.php',
type: 'post',
dataType: 'json',
data: {
}
}).done(function(data) {
$.each(data.emails, function(index, value) {
links.push(value);
console.log(links);
return value;
});
});
function getVal(){
console.log(links[0].target);
}
</script>
<button onclick="getVal()">Get target</button>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句