加载ajax JSON失败
Loading ajax JSON failure
下面的脚本运行在我的本地主机,但不工作在我的网站。我只是在jqueryMobile列表格式测试简单的Json脚本你知道怎么了吗?
感谢HTML页面点击调用Ajax函数:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="protected/jQuery/jquery.mobile.structure-1.0.1.min.css" />
<link rel="stylesheet" href="protected/themes/Green/red_theme.min.css" />
<script src="news_services/js/jquery.js"></script>
<script src="news_services/js/jquery.mobile-1.0rc1.min.js"></script>
<script type="text/javascript" src="protected/jsnM.js"> </script>
</head>
<body >
<div data-role="page" id="home" >
<div data-role="header" >
<h1>JSON testing</h1>
<div data-role="controlgroup" data-type="horizontal"></div>
</div>
<div data-role="content">
<p> Json testing</p>
</div>
<div data-role="footer">
<a href="#getAll_JSON" onClick="jsn1()" data-role="button" data-inline="true" data-icon="info">call JSON</a>
</div>
</div>
<div data-role="page" id="getAll_JSON">
<div data-role="header" data-position="fixed" >
<h1>Json format</h1>
<div data-type="horizontal" >
<a href="#home" data-role="button" data-inline="true" data-icon="home">Home</a>
</div>
</div>
<div data-role="content">
<ul id="sitesList" data-role="listview" data-filter="true" data-split-icon="gear" >
</ul>
</div>
<div data-role="footer" data-position="fixed"></div>
</div>
<div id="detailsPage" data-role="page" >
<div data-role="header">
<h1>info</h1>
<div data-type="horizontal" >
<a href="#home" data-role="button" data-inline="true" data-icon="home">Home</a>
</div>
</div>
<div data-role="content">
<div id="sitesDetail" >
</div>
</div>
</div>
</body>
</html>
调用Ajax:
var areaID=0;
function jsn1(val)
{
var url_Category="http://gonorth.co.il/protected/indexJSON.php?";
$.ajax({
url: url_Category,
type: "GET",
data: 'No='+val+"&area="+areaID,
dataType: "json",
cache: false,
error: function () {
alert('loading Ajax failure');
} ,
onFailure: function () {
alert('Ajax Failure');
} ,
statusCode: {
404: function() {
alert("missing info");
}
},
success: function(result) {
$('#sitesList li').remove();
$.each(result.sites,function(index,dat){
$("#sitesList").append(
'<li>'+
'<img src="images/'+dat.coupon_img+'" width=80/>' +
'<p >Name: '+dat.coupon_name+'</p>'+
'<p >info: '+dat.street+'</p>'+
'<p >address:'+dat.coupon_tmp+'</p>'+
'</li>'
);
});
$('#sitesList').listview('refresh');
}
});
}
PHP结果:<?php
$arr = array();
$arr[] =[
"coupon_id" => '1',
"coupon_img" => '1.jpg',
"street" => 'long text',
"coupon_tmp" => 'Address',
"coupon_name" => 'Name'
];
echo '{"sites":'.json_encode($arr).'}';
?>
当我访问这个链接:http://gonorth.co.il/protected/indexJSON.php时,看起来您的JSON是通过web服务器的PHP解析器提供的。您应该查看web服务器的设置,并找出如何将JSON文件设置为内容类型为application/json
的方式。当前响应的内容类型为text/html
,这很可能是因为它试图被PHP解析器解析,解析器阻塞了语法,或类似的东西,然后服务器呈现一个html错误页面。
感谢所有的答案。问题是Php数组可能是不同的Php版本在我的主机和服务器下一个Php数组在我的本地主机上工作,但在服务器上不起作用
<?php
$arr = array();
$arr[] =[
"coupon_id" => '1',
"coupon_img" => '1.jpg',
"street" => 'long text',
"coupon_tmp" => 'Address',
"coupon_name" => 'Name'
];
echo '{"sites":'.json_encode($arr).'}';
?>
我改成
<?php
$arr[] =array(
"coupon_id" => '1',
"coupon_img" => '1.jpg',
"street" => 'long text',
"coupon_tmp" => 'Address',
"coupon_name" => 'Name'
);
echo '{"sites":'.json_encode($arr).'}';
?>
它在服务器上也能工作
<?php
$arr = array();
$arr[] =[
"coupon_id" => '1',
"coupon_img" => '1.jpg',
"street" => 'long text',
"coupon_tmp" => 'Address',
"coupon_name" => 'Name'
];
$res['sites']=$arr;
echo json_encode($res);
?>
相关文章:
- 当json解析空响应时,Whatwg-Fetch失败,我该如何防止它
- 当其他解析器认为意外的令牌有效时,json.parse会失败
- $.ajax调用失败,dataType为json
- 如何避免JSON.stringify在特殊情况下返回undefined,从而为JSON.parse创建字符串失败
- 当字符串包含 HTML 标记时,JSON 解析失败
- 在什么情况下,应该.deep.equal失败,但使用JSON.stringify进行比较工作正常
- 使用 JavaScript 解析 json 失败
- $http-get方法即使使用AngularJS中.Net WebApi的有效json也会失败
- zombie.js-xhr-json总是失败
- AJAX请求json失败
- 从php获取javascript中的json对象失败
- Angular JS,把json放到ng重复出一个事件失败
- Google 脚本:JSON.parse 无缘无故失败
- JSON 失败并继续执行代码
- d3.json有效,但$.getJson失败
- JSON.parse 失败,并显示有效的 JSON
- 解析 JSON 失败,JavaScript 中缺少元素
- 字符串解析、JSON Stringify 和 GSON 日期解析失败
- JSON.parse无缘无故失败
- JSON.stringify 在新创建的对象(node、javascript)上失败