[英]What is the proper way in getting data from a json file?
What is the proper way in getting a data from my json file? 从我的json文件中获取数据的正确方法是什么? the result is always undefined
结果总是不确定的
Item name: undefined Item key: undefined 项目名称:未定义项目密钥:未定义
.json file .json文件
[{"item_id":"1","item_name":"Book","item_key":"290Q12"}]
my script, jquery 我的脚本,jQuery
var item_name;
var item_key;
var item_id = '<?php echo $row->item_id; ?>';
var get_loc = '<?php echo base_url();?>index.php/main/get_one_item_info/';
item();
function item() {
$(document).ready(function () {
$.ajax({
type: 'GET',
url: get_loc + item_id,
data: { get_param: 'value' },
success: function (data) {
for (var i=0;i<data.length;++i)
{
item_name = data[0].item_name;
item_key = data[0].item_key;
document.getElementById("items").innerHTML = "Item name: " + item_name + "<br>Item key: " + item_key + " ";
}
}
});
});
setTimeout(item, 2500);
};
index.php 的index.php
<div id='items'></div>
You can also try this 您也可以尝试
$url = 'your url';
$obj = json_decode(file_get_contents($url), true);
$item_name = $obj['item_name'];
$item_key = $obj['item_key'];
// print_r($obj);
You can use $.getJSON() method with the file name mentioned on it.... 您可以将$ .getJSON()方法与上面提到的文件名一起使用...。
$.getJSON('link of json file',function(response){
// action to be performed
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.