Reactjs 如何使用承载调用 magento 2 api(服务器响应状态为 401(未授权))
Reactjs how to call magento 2 api with bearer (the server responded with a status of 401 (unauthorized))
我尝试使用 reactjs(来自 localhost:80)调用 (http://m2.example.com:80) magento 2 api,但它只有 return 错误:服务器响应状态为 401(未授权)
import React from "react";
import ReactDOM from "react-dom";
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
items: []
};
}
componentDidMount() {
fetch("http://m2.example.com/rest/V1/products?searchCriteria[page_size]=20",{
method: 'GET',
mode: 'no-cors',
credentials: 'include',
withCredentials: true,
headers: {
'Authorization': 'Bearer 3nsi1y7pcun1atvhs87dxmokymquofii',
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result.items
});
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
render() {
const { error, isLoaded, items } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<ul>
{items.map(item => (
<li key={item.name}>
{item.name} {item.price}
</li>
))}
</ul>
);
}
}
}
/*const App = () => (
<div>
<h1>Hello world!!</h1>
</div>
)*/
ReactDOM.render(<MyComponent/>, document.getElementById("root"));
//ReactDOM.render(<App/>, document.getElementById("root1"));
有人知道上面的代码有什么问题吗?
尝试删除 mode: 'no-cors'
和 'withCredentials: true'
并从后端处理 CORS php magento2 - 请查看此 link 如何操作
fetch("http://m2.example.com/rest/V1/products?searchCriteria[page_size]=20",{
method: 'GET',
credentials: 'include',
headers: {
'Authorization': 'Bearer 3nsi1y7pcun1atvhs87dxmokymquofii',
'Content-Type': 'application/json'
}
之后它应该可以工作了。
我尝试使用 reactjs(来自 localhost:80)调用 (http://m2.example.com:80) magento 2 api,但它只有 return 错误:服务器响应状态为 401(未授权)
import React from "react";
import ReactDOM from "react-dom";
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
items: []
};
}
componentDidMount() {
fetch("http://m2.example.com/rest/V1/products?searchCriteria[page_size]=20",{
method: 'GET',
mode: 'no-cors',
credentials: 'include',
withCredentials: true,
headers: {
'Authorization': 'Bearer 3nsi1y7pcun1atvhs87dxmokymquofii',
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result.items
});
},
// Note: it's important to handle errors here
// instead of a catch() block so that we don't swallow
// exceptions from actual bugs in components.
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
render() {
const { error, isLoaded, items } = this.state;
if (error) {
return <div>Error: {error.message}</div>;
} else if (!isLoaded) {
return <div>Loading...</div>;
} else {
return (
<ul>
{items.map(item => (
<li key={item.name}>
{item.name} {item.price}
</li>
))}
</ul>
);
}
}
}
/*const App = () => (
<div>
<h1>Hello world!!</h1>
</div>
)*/
ReactDOM.render(<MyComponent/>, document.getElementById("root"));
//ReactDOM.render(<App/>, document.getElementById("root1"));
有人知道上面的代码有什么问题吗?
尝试删除 mode: 'no-cors'
和 'withCredentials: true'
并从后端处理 CORS php magento2 - 请查看此 link 如何操作
fetch("http://m2.example.com/rest/V1/products?searchCriteria[page_size]=20",{
method: 'GET',
credentials: 'include',
headers: {
'Authorization': 'Bearer 3nsi1y7pcun1atvhs87dxmokymquofii',
'Content-Type': 'application/json'
}
之后它应该可以工作了。