加载ajax JSON失败

Loading ajax JSON failure

本文关键字:失败 JSON ajax 加载      更新时间:2023-09-26

下面的脚本运行在我的本地主机,但不工作在我的网站。我只是在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);
?>