【发布时间】:2022-01-18 17:26:15
【问题描述】:
我正在尝试在我的 js/svelte 应用程序中发出获取请求。 REST-API 在浏览器中或在使用 postman 进行测试时工作得非常好。
当我点击网站上的 Go 按钮时,检查工具的控制台中会出现以下错误。
Uncaught TypeError: can't convert undefined to object
mergeConfig mergeConfig.js:92 请求 Axios.js:39 方法 Axios.js:129 包装 bind.js:9 登录 Login.svelte:11 听 index.mjs:412 listen_dev index.mjs:1961 挂载 bundle.js:3413 mount_component index.mjs:1745 更新 bundle.js:765 更新 bundle.js:931 更新 index.mjs:1075 刷新 index.mjs:1042 承诺回调* schedule_update index.mjs:1000 make_dirty index.mjs:1777 ctx 索引.mjs:1815 unsubscribeLoc bundle.js:1442 订阅 index.mjs:50 实例$3 Router.svelte:493 初始化索引.mjs:1809 路由器 bundle.js:1583 create_fragment bundle.js:3583 初始化索引.mjs:1824 应用程序包.js:3655 应用程序 main.js:3 bundle.js:3675
这是代码。
<script>
import {replace} from 'svelte-spa-router'
import {LoginDto} from "../scripts/data_transfer_objects/LoginDto";
import axios from "axios";
let loginTemplate = new LoginDto();
function login(){
console.log(loginTemplate.password);
axios.get("http://localhost:5000/login", {
auth: {
username: "test",
password: "1234"
}
});
replace("#/activities");
}
</script>
<div>
<input type="password" placeholder="Password" bind:value={loginTemplate.password}>
<button on:click={login}>Go</button>
</div>
有人知道问题出在哪里吗?
提前致谢!
编辑
我尝试了一个更简单的例子。
<script>
import axios from "axios";
axios.get("localhost:5000/activities");
</script>
这也不起作用。 我产生与上述相同的错误。
【问题讨论】:
-
这也不起作用。我很确定通过 auth: {} 和获取请求是可以的。它在使用邮递员时有效。
-
什么版本的axios?
-
"axios": "^0.24.0",
标签: javascript axios svelte