openweather API를 호출합니다. URL을 브라우저에 입력하면 JSON 객체를 볼 수 있습니다. 그러나 내 아약스는 내 HTML에 아무것도 표시 할 수 없습니다. json 객체 전체를 document.write하는 경우에도 그렇게 할 수 없습니다. 위도와 경도를 출력 할 수 있으므로 지리적 위치가 작동합니다. 이 코드에 무엇이 잘못된 것인지 잘 모릅니다. 여기모든 것이 작동하지만 Ajax는 작동합니다.
<body>
<div class="container">
<div class="col-lg-12 header">
<h1>My Location</h1>
</div>
<div class="row text-center box">
<div class="col-lg-12" id="data">Lat Long</div>
</div>
</div>
</body>
을 JS있어 :
다음은 HTML의
$(document).ready(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(main);
}
});
function main(position){
var lat = position.coords.latitude;
var long = position.coords.longitude;
var api = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + long + "&appid=[redacted key]";
$.ajax({
dataType: 'json',
url: api,
success: function(data) {
city = data.name;
$("#data").html(city);
}
});
}
콘솔에서 도메인 간 요청에 대해 오류가 발생합니까? – nnnnnn
콘솔에서 HTTPS에서 HTTP에 액세스하는 중 오류가 발생 했습니까? (진심으로, 당신은 ** 콘솔에서 SO를 묻기 전에 오류 메시지를 볼 필요가 있습니다 **). – Quentin
콘솔 오류가 발생하지 않았습니다. messgae – Andy