Json HTTP获取不获取
试着这么做却一无所获Json HTTP获取不获取,json,http,get,Json,Http,Get,试着这么做却一无所获 function getWeather() { $.ajax({ type: "GET", url: "http://api.openweathermap.org/data/2.5/weather?q=London", async: false, jsonpCallback: 'jsonCallback', contentType: "application/json", dataType: 'JSON', success:
function getWeather() {
$.ajax({
type: "GET",
url: "http://api.openweathermap.org/data/2.5/weather?q=London",
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'JSON',
success: function(data)
{
$('#jsonp-results').html(JSON.stringify(data));
},
error: function(e)
{
alert(e.message);
}
});
return data; //The JSON response whould be in this so that I can take this and can do some operation
}
单击按钮时,它应该返回JSON
<body>
<button onclick="getWeather();">Get Weather</button>
</body>
我没有犯我所犯的错误
编辑
从此处给出的代码片段中尝试此操作:
<!DOCTYPE html>
<html>
<body>
<p id="temp"></p>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script language="javascript" type="text/javascript">
function getWeather() {
data_Json = {};
$.ajax({
url: "http://api.openweathermap.org/data/2.5/weather?q=London",
dataType: 'JSON',
success: function(data) {
//alert(JSON.stringify(data));
data_Json = data;
//alert("Weather Report: "+data_Json);
},
error: function(e) {
alert(e.message);
}
});
return data_Json;
}
function temp() {
//getWeather();
var obj = JSON.stringify(getWeather());
//alert("Got"+JSON.stringify(obj));
//alert(JSON.stringify(getWeather()));
//document.getElementById("temp").innerHTML = obj.main.temp;
alert("Temp : "+obj);
}
</script>
</body>
<body>
<button onclick="getWeather();">Get Weather</button>
<button onclick="temp();">Temperature</button>
</body>
</html>
函数getWeather(){
数据_Json={};
$.ajax({
url:“http://api.openweathermap.org/data/2.5/weather?q=London",
数据类型:“JSON”,
成功:功能(数据){
//警报(JSON.stringify(数据));
data_Json=数据;
//警报(“天气报告:+data_Json”);
},
错误:函数(e){
警报(e.message);
}
});
返回数据;
}
函数temp(){
//getWeather();
var obj=JSON.stringify(getWeather());
//警报(“Got”+JSON.stringify(obj));
//警报(JSON.stringify(getWeather());
//document.getElementById(“temp”).innerHTML=obj.main.temp;
警报(“温度:+obj”);
}
天气预报
温度
它返回{}
未返回JSON(在getWeather中定义的返回)
不是温度(在get element by id part中)您的html中确实有id=“jsonp results”的元素吗 简化的一些提示:
- 如果不使用jsonp,则不需要指定jsonp回调
- 同步调用不好,因为它们会阻塞,您应该避免这种情况,而且跨域调用甚至不支持同步调用(您的情况就是这样)
function getWeather() {
$.ajax({
url: "http://api.openweathermap.org/data/2.5/weather?q=London",
dataType: 'JSON',
success: function(data) {
$('#jsonp-results').html(JSON.stringify(data));
},
error: function(e) {
alert(e.message);
}
});
}
<body>
<button onclick="getWeather();">Get Weather</button>
<div id="jsonp-results"></div>
</body>
函数getWeather(){
$.ajax({
url:“http://api.openweathermap.org/data/2.5/weather?q=London",
数据类型:“JSON”,
成功:功能(数据){
$('#jsonp results').html(JSON.stringify(data));
},
错误:函数(e){
警报(e.message);
}
});
}
天气预报
在您的评论之后:它当然有效:
您的设置一定有问题。从请求中删除contentType,它就会正常工作 因为这是一个跨源请求,所以不能设置contentType 以下是您将在控制台中看到的错误- 无法加载XMLHttpRequest。访问控制允许标头不允许请求标头字段内容类型 语句
也返回数据代码>将失败,因为变量数据在该上下文中不可用
您可以使用下面的代码-
函数getWeather(){
weatherJson={};
$.ajax({
键入:“获取”,
url:“http://api.openweathermap.org/data/2.5/weather?q=London",
async:false,
JSONPCCallback:'jsonCallback',
数据类型:“JSON”,
成功:功能(数据){
$('#jsonp results').html(JSON.stringify(data));
weatherJson=数据;
},
错误:函数(e){
警报(e.message);
}
});
返回天气预报;
}
您可以尝试此解决方案
function getWeather() {
data_Json = {};
$.ajax({
type: "GET",
url: "http://api.openweathermap.org/data/2.5/weather?q=London",
async: false,
dataType: 'JSONP',
success: function(data) {
$('#jsonp-results').html(JSON.stringify(data));
data_Json = data;
},
error: function(e) {
alert(e.message);
}
});
return data_Json; }
对于编辑零件:-
<!DOCTYPE html>
<html>
<body>
<p id="temp"></p>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
function getWeather() {
data_Json = {};
$.ajax({
url: "http://api.openweathermap.org/data/2.5/weather?q=London",
dataType: 'JSON',
success: function (data) {
data_Json = JSON.stringify(data);
},
error: function (e) {
alert(e.message);
}
});
alert(data_Json);
return data_Json;
}
function temp() {
var obj = getWeather();
alert("Temp : " +obj);
}
</script>
<button onclick="getWeather();">Get Weather</button>
<button onclick="temp();">Temperature</button>
</body>
函数getWeather(){
数据_Json={};
$.ajax({
url:“http://api.openweathermap.org/data/2.5/weather?q=London",
数据类型:“JSON”,
成功:功能(数据){
data_Json=Json.stringify(数据);
},
错误:函数(e){
警报(e.message);
}
});
警报(数据);
返回数据;
}
函数temp(){
var obj=getWeather();
警报(“温度:+obj”);
}
天气预报
温度
事实上,为了能够运行跨站点脚本,您需要使用JSONP。另请参见返回weatherJson返回{},而不是JSON@anupam_on如果将async设置为true,则会出现错误。但由于它是一个同步调用,因此只有在调用success函数后才会返回。与此相关的是,您在success函数中获取数据了吗?我的意思是你只是把警报放在success函数中参见编辑部分,返回的数据实际上什么都没有,所以当我执行并试图从另一个函数获取数据时,它只给出{}
function getWeather() {
data_Json = {};
$.ajax({
type: "GET",
url: "http://api.openweathermap.org/data/2.5/weather?q=London",
async: false,
dataType: 'JSONP',
success: function(data) {
$('#jsonp-results').html(JSON.stringify(data));
data_Json = data;
},
error: function(e) {
alert(e.message);
}
});
return data_Json; }
<!DOCTYPE html>
<html>
<body>
<p id="temp"></p>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
function getWeather() {
data_Json = {};
$.ajax({
url: "http://api.openweathermap.org/data/2.5/weather?q=London",
dataType: 'JSON',
success: function (data) {
data_Json = JSON.stringify(data);
},
error: function (e) {
alert(e.message);
}
});
alert(data_Json);
return data_Json;
}
function temp() {
var obj = getWeather();
alert("Temp : " +obj);
}
</script>
<button onclick="getWeather();">Get Weather</button>
<button onclick="temp();">Temperature</button>
</body>