我试图从JSON URL获取数据我的数据在JSON数组中有数组,我在这里很新,所以请给我一个如何读取数据的建议..谢谢..
这是我的代码..
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Privacy Policy</title>
</head>
<body>
<div id="showdata"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$(function(){
var url = "http://portal.entitledirect.com/test/entitle_news.php";
var data = {"news_type": "docs","dt":"1417222458109"};
$.ajax({
url: url,
data:data,
dataType: "jsonp",
contentType: 'application/json',
type: 'GET',
jsonpCallback: 'methodCaa',
success: function (result) {
console.log(result);
$.each($.parseJSON(result), function (item, value) {
if (item == "rates") {
$.each($.parseJSON(value), function (i, object) {
console.log(i + "=" + object);
});
}
});
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jQuery.parseJSON(jqXHR));
console.log( textStatus ); console.log( errorThrown );
}
});
});
function methodCaa(data){
console.log((data));
}
</script>
</body>
</html>
在从JSON读取值时,我得到错误...
Uncaught TypeError: Cannot read property 'length' of nulljquery-1.7.1.min.js:2 e.extend.eachjsonindex.html:25 $.ajax.successjquery-1.7.1.min.js:2 njquery-1.7.1.min.js:2 o.fireWithjquery-1.7.1.min.js:4 wjquery-1.7.1.min.js:4 d.onload.d.onreadystatechange
答案 0 :(得分:0)
不知道ajax结果的性质以及你想要做什么,这是处理响应的正确语法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Privacy Policy</title>
</head>
<body>
<div id="showdata"></div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$showdata = $('#showdata');
$(function(){
var url = "http://portal.entitledirect.com/test/entitle_news.php";
var data = {"news_type": "docs","dt":"1417222458109"};
$.ajax({
url: url,
data:data,
dataType: "jsonp",
contentType: 'application/json',
type: 'GET',
jsonpCallback: 'methodCaa',
success: function (result) {
console.log(result);
$.each(result, function (item, value) {
console.log("Processing index "+ item);
//if (item == "rates") {
$.each(value, function (i, object) {
$.each(object, function(j, objectValue){
console.log(j, objectValue);
$showdata.append(j +": "+objectValue+"<br />");
});
});
//}
});
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jQuery.parseJSON(jqXHR));
console.log( textStatus ); console.log( errorThrown );
}
});
});
function methodCaa(data){
console.log((data));
}
</script>
</body>
</html>