【问题标题】:JSON / JSONP - works with local json file but not from live external URLJSON / JSONP - 适用于本地 json 文件,但不适用于实时外部 URL
【发布时间】:2012-12-06 01:08:53
【问题描述】:

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

我需要做什么才能让实时 URL 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 方法...并在本地和外部 url 场景中获得相同的结果:

$(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;
            });
        });

【问题讨论】:

    标签: jquery ajax jsonp getjson


    【解决方案1】:

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

    callback(JSONSTRING)
    

    而不仅仅是 JSON 字符串! jQuery 将使用 $.ajax() 的成功回调自动执行这段 Javascript 代码

    【讨论】:

    • 啊,我明白了。谢谢。问题:是否有可能获取实时 url 的内容(只是原始 json 没有包装在 callback() 中,如您所述),将其用作本地变量来解析浏览器中的写出?如果这是有道理的。我会尝试看看运行 Web 服务的人是否可以将回调()添加到它......数据正在提供给我以外的许多其他开发人员,所以也许这是他们最终必须做的事情所有将使用数据的网站?还是有其他不需要我需要研究的方法?
    • 要么服务器需要像callback(JSON)一样返回JSON,或者它必须返回一个带有Access-Control-Allow-Origin的响应头指向你的源URL(或者*,如果每个调用源都应该被覆盖) .对于后者,您必须更改为 dataType "json" 而不是 "jsonp"
    猜你喜欢
    • 2015-10-05
    • 2012-01-21
    • 2016-06-21
    • 2023-03-09
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多