在 Javascript 中获取 JSON
getJSON in Javascript
我是 html 和 javascript.As 的新手,据我所知,当我按 "Get JSON Data" button.But 页面没有给我提示时,以下代码应该会发出警报非常感谢任何 response.Any 帮助。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.getJSON("http://127.0.0.1:5000/2", function(result){
if (result.length == 0){
alert("nothing") ;
}
if (result.length){
alert("success") ;
}
// $("div").append(myObject);
});
});
});
</script>
</head>
<body>
<button>Get JSON data</button>
<div></div>
</body>
</html>
将 JSON 调用替换为
$.getJSON("http://127.0.0.1:5000/2", function(result){
if (result.length == 0){
alert("nothing") ;
}
if (result.length){
alert("success") ;
}
// $("div").append(myObject);
}).fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err )
});
这样你就可以看到哪里出了问题。 javascript 看起来没问题,我怀疑是服务器问题。
您也可以尝试从一些随机来源取回 JSON,例如 http://1882.no/API/business/get/results.php?q=skole&page=0
我怀疑应该是 Cross-domain 的问题。这就是为什么我要求控制台日志。你有几个选择:
根据您的 servlet/backend 响应配置 cross-domain headers。
(例如:如果您使用的是 Servlet:)
response.setHeader('Access-Control-Allow-Origin','*');
使用jsonp回调
$.getJSON("http://example.com/something.json?callback=?", function(result){
//response data are now in the result variable
alert(result);
});
The "?" on the end of the URL tells jQuery that it is a JSONP
request instead of JSON. jQuery registers and calls the callback
function automatically.
- 使用 $.ajax 启用 CORS 或 jsonp
ex:
$.ajax({
url: surl,
data: {
id: id // data to be sent to server
},
dataType: "jsonp",
jsonp: "callback",
jsonpCallback: "jsonpcallback"
});
// Named callback function from the ajax call when event fired.
function jsonpcallback(rtndata) {
// Get the id from the returned JSON string and use it to reference the target jQuery object.
var myid = "#" + rtndata.id;
$(myid).feedback(rtndata.message, {
duration: 4000,
above: true
});
}
- 否则,在您的服务器端下载并配置 "CORS.jar",这将允许 cross-domain 请求。
HOW ?
希望你能有所了解。按照适合你的..
我是 html 和 javascript.As 的新手,据我所知,当我按 "Get JSON Data" button.But 页面没有给我提示时,以下代码应该会发出警报非常感谢任何 response.Any 帮助。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.getJSON("http://127.0.0.1:5000/2", function(result){
if (result.length == 0){
alert("nothing") ;
}
if (result.length){
alert("success") ;
}
// $("div").append(myObject);
});
});
});
</script>
</head>
<body>
<button>Get JSON data</button>
<div></div>
</body>
</html>
将 JSON 调用替换为
$.getJSON("http://127.0.0.1:5000/2", function(result){
if (result.length == 0){
alert("nothing") ;
}
if (result.length){
alert("success") ;
}
// $("div").append(myObject);
}).fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err )
});
这样你就可以看到哪里出了问题。 javascript 看起来没问题,我怀疑是服务器问题。
您也可以尝试从一些随机来源取回 JSON,例如 http://1882.no/API/business/get/results.php?q=skole&page=0
我怀疑应该是 Cross-domain 的问题。这就是为什么我要求控制台日志。你有几个选择:
根据您的 servlet/backend 响应配置 cross-domain headers。 (例如:如果您使用的是 Servlet:)
response.setHeader('Access-Control-Allow-Origin','*');
使用jsonp回调
$.getJSON("http://example.com/something.json?callback=?", function(result){ //response data are now in the result variable alert(result); });
The "?" on the end of the URL tells jQuery that it is a JSONP request instead of JSON. jQuery registers and calls the callback function automatically.
- 使用 $.ajax 启用 CORS 或 jsonp
ex:
$.ajax({
url: surl,
data: {
id: id // data to be sent to server
},
dataType: "jsonp",
jsonp: "callback",
jsonpCallback: "jsonpcallback"
});
// Named callback function from the ajax call when event fired.
function jsonpcallback(rtndata) {
// Get the id from the returned JSON string and use it to reference the target jQuery object.
var myid = "#" + rtndata.id;
$(myid).feedback(rtndata.message, {
duration: 4000,
above: true
});
}
- 否则,在您的服务器端下载并配置 "CORS.jar",这将允许 cross-domain 请求。 HOW ?
希望你能有所了解。按照适合你的..