2012-12-18 27 views
0

我有一种情况,其中我需要从外部Web应用程序我的客户端提供显示一些相当简单的数据(使用jQuery),用作了格式正确json(使用jsonlint.com验证)。这是我第一次使用json,当我使用dataType jsonp和LIVE url时,我在JSON数据的name:value对的开头的第一个引号中出现'invalid label'错误...但是当我复制相同的json并保存在本地,并将jsonp更改为json,它可以工作,我可以在DOM中以任何方式操纵数据,并完成后面的Im。JSON/JSONP - 与当地的JSON文件,但不能从现场外部URL

什么,我需要做的就是现场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场景得到相同reults:

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

回答

2

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

callback(JSONSTRING) 

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

+0

啊,我明白了。谢谢。问题:是否有可能获得实时url的内容(只是原始的json不包裹在callback()中,如你所注意的那样),用它作为局部变量来解析在浏览器中写出的内容?如果这是有道理的。我将尝试查看运行Web服务的人是否可以将callback()添加到其中...将数据提供给除我之外的其他开发人员,因此这可能是最终他们必须做的事情所有将使用这些数据的网站?或者是否有其他方法不需要我需要研究? – tamak

+0

或者服务器需要返回JSON,比如'callback(JSON)',或者它必须发回一个带有Access-Control-Allow-Origin'的响应头指向你的起始URL(或者如果每个调用起源都应该是'*'覆盖)。对于后者,您将不得不更改为dataType“json”而不是“jsonp” – devnull69