成功进行身份验证(登录+令牌)后,我仍然无法请求auth:sanctum路由,并且收到以下响应:
import React, { useState } from "react";
const LoginForm = () => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const loginHandler = (ev) => {
ev.preventDefault();
if (email.length > 0 && password.length > 0) {
axios.get("/sanctum/csrf-cookie").then(() => {
axios
.post("api/login", {
email: email,
password: password,
})
.then((response) => {
console.log(response.data);
})
.catch(function (error) {
console.error(error);
});
});
}
};
Run Code Online (Sandbox Code Playgroud)
public function login(Request $request)
{
$request->validate(['email' => 'required', 'password' => 'required|string']);
$user …Run Code Online (Sandbox Code Playgroud) 我正在使用useRequest图书馆的内容ahooks。我cacheKey在选项中指定了,但是当应该从缓存中检索相同的请求时,它会一遍又一遍地重复。
let someVar = 'someValue'
const { data, loading } = useRequest(
async () => someRequestThatDependOnVariable(someVar),
{
refreshDeps: [someVar],
cacheKey: `request-${someVar}`,
},
);
Run Code Online (Sandbox Code Playgroud)
someVar 通过选择字段更新
我的问题类似于How to post query parameters with Axios? 我不需要发布,而是想要一个获取数据请求,并且我想将查询参数名称传递给请求。它在邮递员中有效,但在反应中无效。
const handleSubmit = async () => {
try {
const res = await axios.get(
"http://localhost:5000/api/products",
{},
{
params: {
name,
},
}
);
console.log(res.data);
} catch (err) {}
};
exports.retrieveProducts = (req, res) => {
Product.find(
{ name: { $regex: req.query.name, $options: "i" } },
(err, products) => {
if (err) res.status(500).json(err);
res.json(products);
}
);
};
Run Code Online (Sandbox Code Playgroud) 您好,我将代码从 fetch 更改为 axios,当我运行测试时,我遇到了这个问题...任何人都可以帮助我吗?
\n语法错误:无法在模块外部使用 import 语句
\n> 1 | import axios from "axios";\nRun Code Online (Sandbox Code Playgroud)\nJest 遇到意外令牌
\nThis usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.\n\nBy default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".\n\nHere's what you can do:\n \xe2\x80\xa2 If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.\n \xe2\x80\xa2 …Run Code Online (Sandbox Code Playgroud) 当我尝试登录我的 React 应用程序时,它返回成功并将其保存在 sessionStorage 中,但是当我尝试访问需要 Bearer 令牌通过 axios 获取数据的页面时,它返回 http 401 错误。
但是当我重新加载页面时,结果就是所需的。
import axios from "axios";
let token = JSON.parse(window.sessionStorage.getItem("token"));
let AxiosInstance= axios.create({
baseURL: "https://myurl.com/backend/api/",
timeout: 5000,
headers: { Authorization: "Bearer " + token },
});
export default AxiosInstance;
Run Code Online (Sandbox Code Playgroud) 我正在尝试根据 api 请求实现全局级预加载器。我刚刚用样品尝试过。但不起作用。如何在全局范围内实现拦截器?
import axios from 'axios';
export default async (searchTerms) => {
try {
const url = `${process.env.SERVICE_URL}/get-jayachan-detais`;
const params = JSON.stringify({
orderInfoRequest: {
searchType: 'MASTER TRX',
MasterTrxnId: searchTerms.ordnum
}
});
const response = await axios.post(url, params, { withCredentials: true });
return {
errors: [],
response
};
} catch (e) {
return {
errors: [e]
};
}
};
axios.interceptors.request.use(function () {
console.log('start preloader');
});
axios.interceptors.response.use(function () {
console.log('stop preloader');
});
Run Code Online (Sandbox Code Playgroud) 我有这个vue2代码:
checkUser2() {
var returnValue;
axios.get(`api/users/CheckUsername/${this.username}`)
.then(response => {
returnValue = response.data.error === 0;
}, errorCallBack => {
returnValue = false;
});
return returnValue;
}
Run Code Online (Sandbox Code Playgroud)
我叫它:
var a = this.checkUser2();
console.log(a);
Run Code Online (Sandbox Code Playgroud)
它总是返回undefined.我究竟做错了什么?
我想使用返回objectId数组列表的axios来获取API.在我获得列表objectId之后,我想使用promise获取对象的细节
我想到这样的事情
var objectDetail = [];
axios.get('apiendpoint/')
.then((response) => {
var listOfObjectId = response.data;
var chain = Promise.resolve()
for (var objectId of listOfObjectId) {
chain = chain.then(axios.get(`apiendpoint/${objectId}`)
.then((response) => {
objectDetail.push(response.data);
})
);
}
return chain;
}).then((chain) => {
console.log(chain);
return chain;
})
Run Code Online (Sandbox Code Playgroud)
上面的代码返回undefined,promise链对象不传递给then方法调用.我的方法错了还是我错过了什么?谢谢
这是我读过的一些堆栈,可能是相关的:
我正在axiosReact Native中使用发帖请求来执行登录用户功能。我成功获得响应,但是登录后我无法导航到主屏幕。这是我的代码。
axios.post('/wp-json/api/v1/user/do_login', {
username: username,
password: password
})
.then(function (response) {
console.log(JSON.stringify(response.data.type));
if(response.data.type == "success"){
alert("Login Successfully");
this.props.navigation.navigate("home");
}else if(response.data.type == "error"){
alert("Incorrect Detail");
}
})
.catch(function (error) {
console.log( JSON.stringify(response));
});
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我获得了成功的响应,但没有得到下一个屏幕。这是我导航到下一个屏幕..
this.props.navigation.navigate("home");
Run Code Online (Sandbox Code Playgroud) 异步Axios调用让我有些失落。
我嵌套了Axios调用,这些调用工作得很好,但是我发现它不可读,并且我经常在这段代码中迷失了自己。
现在,我有类似的东西:
axios.get().then((response) => {
this.pbmInfos = response.data;
axios.get().then((response) => {
this.contactInfos = response.data;
axios.get().then((response) => {
this.clientInfos = response.data).catch((error) => console.log(error));
}).catch((error) => console.log(error));
axios.get().then((response) => {
this.lastViewDate = response.data).catch((error) => console.log(error));
}).catch((error) => console.log(error));
Run Code Online (Sandbox Code Playgroud)
(当然,为了清楚起见,我没有在get()中编写URL)
就目前而言,它看起来似乎并不太复杂,但这只是我打电话的一部分。
我试图在通用函数中将其分解,以便可以使用不同的参数来调用它,但是我没有成功,因为每个调用都需要将获取的数据分配给另一个变量,并且某些调用还需要在成功时调用另一个调用。
如果您有任何想法,我很乐意阅读。
axios ×10
reactjs ×6
javascript ×4
vue.js ×3
asynchronous ×1
laravel ×1
node.js ×1
promise ×1
react-hooks ×1
react-native ×1
typescript ×1