Я использую axios с React и Node.js, но результат отличается.
При реакции я получил ошибку CORS, но с node.js я получил ответ правильно.
Пример кода приведен ниже:
С React
import React, { useEffect } from 'react';
import axios from 'axios';
import hmacGenerator from '../utils/hmacGenerator';
const REQUEST_METHOD = 'POST';
const DOMAIN = 'https://api-gateway.exmple.com';
const URL = '/deeplink'
const ACCESS_KEY = 'xxxxxx';
const SECRET_KEY = 'xxxxxx';
const REQUEST = {
coupangUrls: [
'https://www.example.com/search?component=&q=good&channel=user'
]
};
const App = () => {
useEffect(() => {
(async () => {
try {
const authorization = hmacGenerator(
REQUEST_METHOD,
URL,
SECRET_KEY,
ACCESS_KEY
);
console.log(authorization);
axios.defaults.baseURL = DOMAIN;
const response = await axios.request({
method: REQUEST_METHOD,
url: URL,
headers: { Authorization: authorization },
data: REQUEST
});
console.log(response);
} catch (err) {
console.error(err.response);
}
})();
}, []);
return <div>App</div>;
};
export default App;
С Node.js
const axios = require('axios');
const { generateHmac } = require('./hmacGenerator');
const REQUEST_METHOD = 'POST';
const DOMAIN = 'https://api-gateway.exmple.com';
const URL = '/deeplink'
const ACCESS_KEY = 'xxxxxx';
const SECRET_KEY = 'xxxxxx';
const REQUEST = {
coupangUrls: [
'https://www.example.com/search?component=&q=good&channel=user'
]
};
(async () => {
const authorization = generateHmac(
REQUEST_METHOD,
URL,
SECRET_KEY,
ACCESS_KEY
);
axios.defaults.baseURL = DOMAIN;
console.log(authorization);
try {
const response = await axios.request({
method: REQUEST_METHOD,
url: URL,
headers: { Authorization: authorization },
data: REQUEST
});
console.log(response.data);
} catch (err) {
console.error(err.response.data);
}
})();
Почему Node.js не сделалне получается ошибка CORS?
Если вы не получили мой вопрос, пожалуйста, оставьте комментарий.
Пожалуйста, дайте мне знать.
Спасибо.