简体   繁体   English

如何修复“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

When i try to fetch data from api( https://openweathermap.org/ ) i get this error.当我尝试从 api( https://openweathermap.org/ )获取数据时,我收到此错误。 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0

this is my code.这是我的代码。

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;

thanks!谢谢!

You are getting a JSON back.您将得到一个 JSON 回来。 I just tried to call我只是试着打电话

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

}

It responds with:它回应:

{
"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
}

You might have missed the http:// part?您可能错过了http://部分?

i had the same issue in my Ionic app, i just added the './' before the url and it works for me: fetch('assets/files/data.json') => fetch('./assets/files/data.json')我在 Ionic 应用程序中遇到了同样的问题,我只是在 url 之前添加了“./”,它对我有用: fetch('assets/files/data.json') => fetch('./assets/files/数据.json')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获(承诺)SyntaxError:JSON 中位置 0 的意外标记 &lt; - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 Redux 应用程序错误:未捕获(承诺中) SyntaxError:意外令牌 &lt; 在 position 0 处的 JSON - Redux app error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 错误语法错误:令牌异常 - ERROR Uncaught SyntaxError: Unexpected token <in JSON at position 0 如何解决 Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 控制台错误? - How to solve Uncaught SyntaxError: Unexpected token < in JSON at position 0 console error? Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 使用 vuejs - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 using vuejs Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 (React/Redux App) - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 (React/Redux App) React Js: Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - React Js: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 VM101:1 Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - VM101:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 未捕获(承诺)SyntaxError:意外令牌 < in JSON at position 0 来自 SpringBoot API - Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 from SpringBoot API VM39:1 Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0 - VM39:1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM