我想发送一个带对象的get请求.将在服务器上使用对象数据来更新会话数据.但是这个对象似乎没有被正确发送,因为如果我尝试将其发回去打印出来,我只会得到:
" N; "
我可以用这样的jQuery来做它并且它有效:
$.get('/mysite/public/api/updatecart', { 'product': this.product }, data => {
console.log(data);
});
Run Code Online (Sandbox Code Playgroud)
该对象通过laravel从服务器发送回来,如下所示:
public function updateCart(Request $request){
return serialize($request->product);
Run Code Online (Sandbox Code Playgroud)
同样的事情不适用于axios:
axios.get('/api/updatecart', { 'product': this.product })
.then(response => {
console.log(response.data);
});
Run Code Online (Sandbox Code Playgroud)
我用axios设置了一个默认的baseURL,所以url是不同的.它正确到达api端点,函数返回发送的内容,这显然不是对象.结果我只得到" N ".
我正在使用 Next.js 构建一个应用程序,我需要连接到特定的 API 路由(使用API Platform设置)并使用路由的响应填充页面。
该API工作正常,但无论我如何努力实现内部的我爱可信电话getServerSideProps,我总是得到同样的错误,ECONNREFUSED从我的节点栈。
我试图从中获取数据useEffect()并且它工作正常,但我想知道是否有办法直接在getServerSideProps.
我正在为 Docker 使用 Node 容器,并且路由通过 JWT 令牌(存储在会话和服务器端连接的客户端 cookie 中)进行身份验证
这是我的代码:
pages/accounts.js:
export async function getServerSideProps(context) {
const cookies = new Cookies(context.req.headers.cookie)
const adminToken = cookies.get('jwtToken')
const res = await getAllAccounts(adminToken)
return {
props: {
testdata: ''
},
}
}
Run Code Online (Sandbox Code Playgroud)
lib/accounts.js:
import service from '../service'
export const getAllAccounts = async (adminToken) => {
const res = service({ jwtToken : adminToken }).get(`/accounts`).then((response) …Run Code Online (Sandbox Code Playgroud) 我在尝试学习在后端使用 axios 发出请求时,在这个小型 Nodejs 应用程序中遇到了此错误。“意外的文件结束”。
import axios from "axios";
export const getData = async () => {
let data;
try {
const response = await axios.get(
"https://jsonplaceholder.typicode.com/users"
);
console.log(response);
data = response;
} catch (error) {
console.log(error.message);
return;
}
return data;
};
Run Code Online (Sandbox Code Playgroud)
import express from "express";
import cors from 'cors';
import axios from "axios";
import { getData } from "./utils/getData.js";
const app = express();
app.use(cors());
app.use(express.json());
app.get("/", async (req, res) => {
let users;
users = await getData(); …Run Code Online (Sandbox Code Playgroud) 我正在使用axios promise库,但我认为我的问题更为普遍.现在我循环遍历一些数据并在每次迭代时进行一次REST调用.
每次调用完成后,我需要将返回值添加到对象.在高层次上,它看起来像这样:
var mainObject = {};
myArrayOfData.forEach(function(singleElement){
myUrl = singleElement.webAddress;
axios.get(myUrl)
.then(function(response) {
mainObject[response.identifier] = response.value;
});
});
console.log(convertToStringValue(mainObject));
Run Code Online (Sandbox Code Playgroud)
当然发生了什么事情,当我打电话console.log给mainObject它时还没有任何数据,因为axios仍在伸出手.处理这种情况的好方法是什么?
Axios确实有一个all方法和一个姐妹方法spread,但如果你提前知道你将要拨多少个电话,它们似乎是有用的,而在我的情况下,我不知道会有多少循环迭代.
我正在使用axios-cookiejar-support库.
我有一个包含正文的POST,由于某种原因,Cookie没有被注入到请求中.我在这做错了什么:
return axios
.post(
urlJoin(
config.portal.url,
'Account/Register'),
{
UserName: "testing_engine@test.com",
UserFirstName: "First Name",
UserLastName: "Last Name",
Email: "testing_engine@test.com",
Password: "...",
ConfirmPassword: "..."
},
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err))
Run Code Online (Sandbox Code Playgroud)
奇怪的是,如果我对相同的端点执行GET,则会传递Cookie:
return axios
.get(
urlJoin(
config.portal.url,
'Account/Register'),
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err));
Run Code Online (Sandbox Code Playgroud)
此外,如果我执行没有正文的POST,它们会被传递:
.post(
urlJoin(
config.portal.url,
`Account/LoginApi?UserName=${config.portal.userName}&Password=${config.portal.password}`),
null,
{
jar: cookieJar,
withCredentials: true
})
.then(res => callback())
.catch(err => callback(err))
Run Code Online (Sandbox Code Playgroud)
import axios from 'axios' …Run Code Online (Sandbox Code Playgroud) 我在React Web应用程序中使用Axios进行API调用.但是我在Chrome中收到错误,
XMLHttpRequest无法加载https://example.restdb.io/rest/mock-data.请求的资源上不存在"Access-Control-Allow-Origin"标头.因此,不允许来源" http:// localhost:8080 "访问.
{
axios.get('https://example.restdb.io/rest/mock-data', {
headers: {
'x-apikey': 'API_KEY',
},
responseType: 'json',
}).then(response => {
this.setState({ tableData: response.data });
});
}
Run Code Online (Sandbox Code Playgroud)
我还在StackOverflow上读了几个关于同一问题的答案,标题为"Access-Control-Allow-Origin",但仍然想弄清楚如何解决这个问题.我不想使用扩展程序IN Chrome或使用临时黑客来解决这个问题.请提出解决上述问题的标准方法.
在尝试了几个答案后,我尝试了这个,
headers: {
'x-apikey': '59a7ad19f5a9fa0808f11931',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
},
Run Code Online (Sandbox Code Playgroud)
现在我收到错误了,
请求标头字段Access-Control-Allow-Origin在预检响应中不允许使用Access-Control-Allow-Origin
我正在运行一个简单的 API 请求,将数据返回到我编写的一个简单的 API 搜索中。我说这是简单的 API 调用,因为不需要身份验证,我可以非常简单地在 python 中完成。
但是,我在 React 中使用 Axios 时遇到了问题。
代码:
axios.get('https://www.keyforgegame.com/api/decks/59554ab7-b414-4220-8514-408a0dbf572d')
Run Code Online (Sandbox Code Playgroud)
我试过看这里,每个人都让它听起来很简单,但我似乎什么也做不了。我试过了。
axios.get('https://www.keyforgegame.com/api/decks/59554ab7-b414-4220-8514-408a0dbf572d', { crossDomain: true })
Run Code Online (Sandbox Code Playgroud)
和
axios.get('https://www.keyforgegame.com/api/decks/59554ab7-b414-4220-8514-408a0dbf572d, {
headers: {
'Access-Control-Allow-Origin': true,
},
})
Run Code Online (Sandbox Code Playgroud)
但我不断收到类似的错误
Access to XMLHttpRequest at 'https://www.keyforgegame.com/api/decks/59554ab7-b414-4220-8514-408a0dbf572d' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
或者
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我是否需要在标题中放一些东西才能完成这项工作?或者这是我需要做出反应的某种设置。
帮助!
我正在使用 axios.create() 传入一个 baseURL 和一些像这样的默认查询参数
axios.create({
baseURL: 'http://somebigurlhere',
params: {
part: 'part',
maxResults: 5,
key: 'key'
}
});
Run Code Online (Sandbox Code Playgroud)
当我使用
axios.get('/search', {
params: {
q: 'word'
}
});
Run Code Online (Sandbox Code Playgroud)
默认参数不会在 GET 调用中合并。
我得到的是
http://somebigurlhere/search?q=word
代替
http://somebigurlhere/search?part=part&maxResults=5&key=key&q=asd
我尝试以许多其他方式放置配置,但它仍然不起作用。我在这里做错了吗?
我在其他项目中尝试过同样的方法,它在那里工作。刚刚用 create-react-app 创建了一个新的反应应用程序,这似乎不再起作用了。
我正在开发一个反应应用程序。我在哪里请求 AXIOS 的 API。但是当我运行 NPM START 在本地主机中测试我的应用程序时,我收到 CORS 错误。这是错误 Access to XMLHttpRequest at 'https://********.com/trx_status.php' from origin ' http://localhost:3000 ' 已被 CORS 策略阻止:请求标头字段私钥预检响应中的 Access-Control-Allow-Headers 不允许。
我是反应新手。请告诉我如何解决这个问题。谢谢...
Like the title, I want to add Axios into Vue prototype. So when I want to use it, I can use it like this.$axios instead of importing it every time.
CODE:
//plugins/axios.ts
import axios from 'axios'
import router from '../router/index'
const errorHandle = (): void => {};
const instance = axios.create({
// baseURL: process.env.NODE_ENV == 'development' ? '' : ''
baseURL: 'http://localhost:3000',
timeout: 1000 * 12
});
instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
export default instance
Run Code Online (Sandbox Code Playgroud)
import { createApp } from 'vue'
import …Run Code Online (Sandbox Code Playgroud)