ApI 调用身份验证给我“无法加载资源:net::ERR_CONNECTION_REFUSED”

问题描述 投票:0回答:1

我有一个 Vue 和 .net 应用程序。 当我运行“NPM startserve”时,它给了我构建

应用程序运行于:- 本地:http://localhost:8080/

但是,当我尝试登录时,它给了我这个错误:

控制台错误:
无法加载资源:net::ERR_CONNECTION_REFUSED localhost:50463/user/authenticate:1

当我按照我的教导更新 axios.service.js 文件以使用 8080 端口时,这将修复它给我的错误,CORS 标头不存在。为什么更改端口会产生这种效果?我该如何解决这个问题?

如果我的问题不正确或没有意义,我很抱歉我是 .net 和 Vue 的新手,但这似乎是一个路由问题?我不确定,因为代码是由其他开发人员编写的,而且我不那么熟练。

axios.service.js

const API_URL = 'http://localhost:50463/';
axios.defaults.baseURL = API_URL;
axios.defaults.headers.post['Content-Type'] = 'application/json';

axios.interceptors.response.use(
    (a) => a,
    (error) => {
        if (!error.response) return Promise.reject({response: {data: 'Network Error'}});
        else if (error && error.response && error.response.status === 401) {
            store.dispatch('authentication/logout');
            router.push('/');
            window.location.replace('/');
            //
        }
        return Promise.reject(error);
    }
);

export const SetBearerToken = function(token) {
    axios.defaults.headers.common['Authorization'] = token ? `Bearer ${token}` : null;
};

export const FilesUrl = API_URL + 'file/';

我尝试更改 API URL 中的端口。

我尝试将登录的用户添加到数据库中,但需要一些字段。 然后我使用了数据库中已有的用户详细信息,但仍然收到相同的错误。

javascript c# .net vue.js routes
1个回答
0
投票

确保您的前端和后端都在运行。 ERR_CONNECTION_REFUSED 看起来 API 可能未运行。 看起来您的 API 可能正在 localhost:50463 上运行,并且您的前端托管在 localhost:8080 上。

当您将端口更改为 8080 时,它会将前端指向自己,因为前端托管在 localhost:8080 上。

启动前端:

npm serve

启动后端:

dotnet run
© www.soinside.com 2019 - 2024. All rights reserved.