JSON / JSONP - 适用于本地json文件,但不适用于实时外部URL

时间:2012-12-18 18:53:24

标签: jquery ajax jsonp getjson

我有一个场景,我需要从我的客户端提供的外部Web应用程序显示一些相当简单的数据(使用JQUERY),提供格式良好的json(使用jsonlint.com验证)。这是我第一次使用json,当我使用dataType jsonp和LIVE url时,我在名称开头的第一个引号上出现“无效标签”错误:JSON数据中的值对...但是当我复制时相同的json并将其保存在本地并将jsonp更改为json,它可以工作,我可以在DOM中以任何方式操作数据并完成我之后的操作。

我需要做些什么才能让实时网址json正常工作?

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>JSON Test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script>
$.ajax({
type : 'GET',
//url : 'jsondata2.js',
url: 'http://.....'    //MY LIVE URL HERE.   NOTE, THE LOCAL FILE ABOVE THAT DOES WORK (JSON NOT JSONP) IS JUST A COPY/PASTE OF THE DATA THAT THIS URL DISPLAYS IN BROWSER.  ,
dataType : 'jsonp',
success : function(jsonData) {
alert(jsonData.PropertyName);
},
error : function() {
alert('Error loading the JSON data');
}
});
</script>
</head>
<body></body>
</html>

继承人的json数据:

{
"PropertyID": 1468,
"PropertyName": "AG Test One",
"Listing Information": {
    "Availability": "Not Specified",
    "Pricing Information": "For Sale, Price: 1.00 (Set Price per Acre)"
},
"Location": {
    "City": "ZZ",
    "County": "ZZ Test County",
    "Municipality": "Within City Limits",
    "State": "SC",
    "Tax Map ID": "123-123-123",
    "ZipCode": "22222"
},
"Physical Details": {
    "Certified Property": false,
    "Minimum Divisible Acreage": 0,
    "Site Comments": null,
    "Site Improvements": "Forested",
    "Surrounding Land Use": [
        "North: Airport / Port / Intermodal Facility",
        "South: Waterway (River / Lake)",
        "East: Utility (Easements / Substations)",
        "West: Correctional Facility"
    ],
    "Total Site Size": 500,
    "Zoning": null
},
"Transportation": {
    "Barge Access": false,
    "Name of Rail Carrier Providing Service": "None specified",
    "Nearest Commercial Airport": "None Specified",
    "Nearest Interstate": "None Specified",
    "Nearest Sea Port": "None Specified",
    "Railway Access": false,
    "Runway Access": false
},
"Utilities": {
    "Diameter of Waste Water Main": 0,
    "Diameter of Water Main": 0,
    "Distance To Electric Provider": 1,
    "Distance to Natural Gas Provider": 0,
    "Distance to Sewer Service": 0,
    "Distance to Water Service": 0,
    "Electric Provider": "Undetermined",
    "Fire Insurance Rating": "1",
    "Natural Gas Provider": null,
    "Telecommunications Providers": null,
    "Type of Sewer": "",
    "Water Service Provider": "Undetermined",
    "Water Water Service Provider": "Undetermined"
}
}

我也尝试了getJSON方法......并在本地和外部网址场景中得到相同的结果:

$(document).ready(function() {
            $.getJSON('jsondata2.js', function(data) {
                //$.getJSON('SAME LIVE URL HERE....', function(data) {
                var output = "<ul>";
                output += "<li>" + data.PropertyName + "</li>";
                output += "</ul>";
                document.getElementById("placeholder").innerHTML = output;
            });
        });

1 个答案:

答案 0 :(得分:2)

对于JSONP,您的服务器需要输出

callback(JSONSTRING)

而不仅仅是JSON字符串! jQuery将使用$ .ajax()

的成功回调自动执行此Javascript代码