![](/img/trans.png)
[英]Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
[英]How to fix “Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0” ERROR
當我嘗試從 api( https://openweathermap.org/ )獲取數據時,我收到此錯誤。 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
這是我的代碼。
import React from 'react';
import './App.css';
import Weather from "./components/Weather"
import 'bootstrap/dist/css/bootstrap.min.css'
import 'weather-icons/css/weather-icons.css'
const Api_Key="079b76b390ad70c628a14a9a141e5992";
class App extends React.Component {
constructor(){
super();
this.state={};
this.getWeather();
}
getWeather= async ()=>{
const api_call = await fetch(
`api.openweathermap.org/data/2.5/weather?q=London,uk&appid=${Api_Key}`,
);
const data = await api_call.json();
console.log(data);
}
render()
{
return (
<div className="App">
<Weather/>
</div>
)
}
}
export default App;
謝謝!
您將得到一個 JSON 回來。 我只是試着打電話
async function get() {
try {
const res = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=079b76b390ad70c628a14a9a141e5992`);
const json = await res.json();
console.log('json', json)
} catch (err) {
console.error('err', err);
}
}
它回應:
{
"coord": {
"lon": -0.13,
"lat": 51.51
},
"weather": [
{
"id": 520,
"main": "Rain",
"description": "light intensity shower rain",
"icon": "09d"
}
],
"base": "stations",
"main": {
"temp": 285.3,
"pressure": 1004,
"humidity": 93,
"temp_min": 284.15,
"temp_max": 286.48
},
"visibility": 10000,
"wind": {
"speed": 6.2,
"deg": 90
},
"clouds": {
"all": 90
},
"dt": 1571056651,
"sys": {
"type": 1,
"id": 1502,
"message": 0.0096,
"country": "GB",
"sunrise": 1571034113,
"sunset": 1571073060
},
"timezone": 3600,
"id": 2643743,
"name": "London",
"cod": 200
}
您可能錯過了http://
部分?
我在 Ionic 應用程序中遇到了同樣的問題,我只是在 url 之前添加了“./”,它對我有用: fetch('assets/files/data.json') => fetch('./assets/files/數據.json')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.