简体   繁体   English

获取 Open Weather API(地理位置)时出现错误 400 错误请求

[英]Error 400 Bad request when getting Open Weather API (geolocation)

I want to get the weather API to display the current location but it gives back an 400 error.我想获取天气 API 以显示当前位置,但它返回 400 错误。 I don't know how to fix it.我不知道如何解决它。 (I open my api on browser and its works fine. But when I put it in the code bellow, it's gave error) (我在浏览器上打开我的 api 并且它工作正常。但是当我把它放在下面的代码中时,它给出了错误)

state: {
        weather:{},
        api_key: 'c9f562aaffcdefaf40f2ed808d202c9b',
        url_base: 'https://api.openweathermap.org/data/2.5/',
        query: '',
        icons: {
            'Rain': require('../assets/regnerisch.png'),
            'Clear': require('../assets/sonne.png'),
            'Clouds': require('../assets/wolkig.png'),
            'Snow': require('../assets/schnee.png'),
            'Drizzle':require('../assets/regnerisch.png'),
            'Thunderstorm':require('../assets/regnerisch.png')
        },          
    },

 actions:{
         currentCity({commit}) {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(position => {
                axios
                .get(`${this.url_base}weather?&lat=${position.coords.latitude}&lon=${position.coords.longitude}&units=metric&APPID=${this.api_key}`)
                .then(res=>res.data)
                .then(weather=>{
                    commit('currentCity',weather)
                })            
            })
        }
    }
},
     mutations:{
         currentCity (state, weather){
             state.weather = weather
         }
     }

400 error means your request was malformed. 400 错误意味着您的请求格式错误。 Since all of your payload data is storedas a query string I assume your url is wrong, Is this part below meant to be backticked?由于您的所有有效负载数据都存储为查询字符串,我假设您的 url 是错误的,下面的这部分是否意味着要反引号? ${this.url_base}weather?&lat=${position.coords.latitude}&lon=${position.coords.longitude}&units=metric&APPID=${this.api_key} ? ${this.url_base}weather?&lat=${position.coords.latitude}&lon=${position.coords.longitude}&units=metric&APPID=${this.api_key} ? Is yes then consider logging the variables to make sure you're getting valid input to the query string是吗然后考虑记录变量以确保您获得对查询字符串的有效输入

You are using undefined variables inside axios call that is why its giving 400 bad request because of wrong url.您在 axios 调用中使用了未定义的变量,这就是为什么由于错误的 url 而给出 400 错误请求的原因。 Working code in GitHub repo after cleaning up home and temperature components.清理家庭和温度组件后,GitHub repo 中的工作代码。 https://github.com/manojkmishra/weather_app_vuex_test https://github.com/manojkmishra/weather_app_vuex_test
demo- https://weather-app-vuex-test.netlify.app/演示- https://weather-app-vuex-test.netlify.app/

Modify store.js as below修改 store.js 如下

import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';

Vue.use(Vuex);
export default new Vuex.Store({
state: { weather:{},    },
 actions:{
    async currentCity ({commit}, position)
    {   let url_base= 'https://api.openweathermap.org/data/2.5/';
        let api_key= 'c9f562aaffcdefaf40f2ed808d202c9b';
        console.log('store-pos',position,url_base,api_key)
        let res=await axios.get(`${url_base}weather?&lat=${position.coords.latitude}&
        lon=${position.coords.longitude}&units=metric&APPID=${api_key}`)
            commit('CUR_CITY',res.data)
        return res;
    },

 },
 mutations:{
     CUR_CITY (state, weather)
     {   console.log('store-muta-state=',state)
         state.weather = weather

     }
 },
  getters:{}
})

modify home.js as below, cleanup your home.vue to test for only one component for eg-temperature and remove all others to check if everything is working.修改 home.js 如下,清理你的 home.vue 以仅测试一个组件,例如温度并删除所有其他组件以检查一切是否正常。

import 'chartjs-plugin-datalabels';
import Temperature from '../temperature/Temperature.vue';
import { mapState} from 'vuex';
import '../../custom.scss';

export default {
computed: 
{  ...mapState({ weather: state => state.weather, 
                }),
},
components: {Temperature},
mounted() {
    navigator.geolocation.getCurrentPosition((position) => 
    {
        this.$store.dispatch('currentCity', position)
    });
},
};

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

相关问题 尝试链接 API 时收到“400 错误请求” - Getting a '400 Bad Request' when I try to link API 在链接获取错误中使用指标时打开天气API - Open weather api when using metrics in link getting error MEAN:在POST上获取错误400(错误请求) - MEAN : Getting Error 400 (Bad Request) on POST jQuery Ajax API错误:400错误的请求 - Jquery Ajax API Error: 400 Bad Request 创建用户时收到错误请求错误请求 400(Firebase) - Getting a bad request bad request 400 when creating user(Firebase) 向Yahoo Finance API发出JSONP请求时,收到400错误请求 - Getting 400 Bad Request when making JSONP request to Yahoo Finance API 如何在获取“ httpStatus 400-错误请求”时解决POST API调用? - How to fix a POST API call when getting 'httpStatus 400 - Bad Request'? 当我尝试使用jQuery从Twitter API获取数据时收到错误请求(400) - Getting a Bad Request (400) when I try to GET data from the Twitter API using jQuery 使用 Fetch API 提交 WordPress 表单时,我不断收到 400(错误请求) - I keep getting a 400 (Bad Request) when submitting a WordPress form using the Fetch API 使用我的 REST API 不断收到 DELETE 400(错误请求) - Keep getting a DELETE 400 (Bad Request) with my REST API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM