小编Sah*_*ata的帖子

尝试使用expressJS重定向时“被CORS策略阻止”

我在尝试使用 ExpressJS 重定向时遇到问题,我制作了一个登录系统来获取发布请求,如果用户存在,则页面应该重定向,但 Cors 却阻止了它。

这是请求:

router.post('/login', async (req, res) => {

    try{    
       let usersData = await  getFiles(__dirname + '/users.json');
       let parsedUsers = JSON.parse(usersData);
       let userChecker;
       for(let i = 0; i < parsedUsers.length; i++){
       if(parsedUsers[i].userName === req.body.userName){
           userChecker = 1;
           break;
       } 
       }

       if(!userChecker){
        console.log(`${req.body.userName} Not exist`);}

        else {
        console.log(`${req.body.userName} Approved`);
        res.redirect('/')
       }
        }
     catch (err) {
      if(err) throw err
    };
})
Run Code Online (Sandbox Code Playgroud)

这是它在控制台发送的错误:

Access to fetch at 'http://localhost:3001/users/login' (redirected from 'http://localhost:4000/users/login') from origin 'null' has been blocked by CORS policy: …
Run Code Online (Sandbox Code Playgroud)

javascript express

7
推荐指数
1
解决办法
4832
查看次数

克隆到另一台 PC 后,Angular 无法正常工作

所以我买了一台新电脑,我想通过 github 传递我的 angular 项目。我用了 1. npm i2. npm i @angular/cli@latest -D3.ng update

每次我尝试使用ng s它运行项目时,它都会向我发送此错误:

This version of CLI is only compatible with Angular versions ^9.0.0-beta || >=9.0.0 <10.0.0,
but Angular version 8.2.14 was found instead.

Please visit the link below to find instructions on how to update Angular.
https://angular-update-guide.firebaseapp.com/
Run Code Online (Sandbox Code Playgroud)

我尝试使用链接中的说明。

这是我输入时的 Angular 版本ng --version

Angular CLI: 9.0.1
Node: 12.16.0
Run Code Online (Sandbox Code Playgroud)

这是 package.json:

{
  "name": "empire-gaming",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng …
Run Code Online (Sandbox Code Playgroud)

node.js angular

6
推荐指数
2
解决办法
1万
查看次数

尝试进入 React 应用程序路由时出现 404

我刚刚在 c-panel 上部署了我的 React 应用程序构建。该应用程序包括不同的路线,每次我尝试到达其中一条路线时,我都会得到404 Not found。例如,如果我尝试访问http://example.com/它,它将进入该网站,如果我按下链接到它的按钮,http://example.com/articles它将起作用。http://example.com/articles但是,如果我尝试从我共享的链接获取或仅输入此地址,我会得到404 Not found. 当我在本地主机上运行开发模式时,不会发生这种情况。

我更改了 package.json 中的主页 url "homepage": "http://example.com",,但没有效果。

我的应用程序根目录包含<Router>

function App() {
  return (
    <Provider store={store}>
      <Router>
        <React.Fragment>
          <CssBaseline />
          <Header title="exampletitle" />
          <MobileHeader />
          <Main />
          <BottomNavbar />
        </React.Fragment>
      </Router>
    </Provider>
  );
}
Run Code Online (Sandbox Code Playgroud)

这是由路由操纵的 Main.js 组件。

function Main(props) {
  return (
    <div>
      <Switch>
        <Route exact path="/" component={Homepage} />
        <Route exact path="/about" component={About} />
        <Route exact path="/signup" component={Registerpage} />
        <Route …
Run Code Online (Sandbox Code Playgroud)

nodes reactjs react-router

6
推荐指数
1
解决办法
1万
查看次数

componentDidMount 未呈现

我有一个 Header 组件,它假设根据用户是否登录的条件来呈现他的子组件。它通过会话存储识别条件。我试图通过以下方式控制渲染 componentDidMount

renderUserHeader = () => {
  if (sessionStorage.getItem('user-auth')) {
    var tokenToSend = { token: sessionStorage.getItem('user-auth') }
    var regJSONED = JSON.stringify(tokenToSend)

    fetch('http://localhost:4000/users/token', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: regJSONED
    })
    .then(response => {
      if (!response.ok) {
        throw new Error('HTTP error ' + response.status)
      }
      return response.text()
    })
    .then(data => {
      let JsonedUserName = JSON.parse(data)

      this.setStateUserName(JsonedUserName.name, JsonedUserName.admin)
    })

    if (!this.state.admin) {
      return <UserHeader name={this.state.userName} />
    } else if (this.state.admin) {
      return <AdminHeader name={this.state.userName} />
    } …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

1
推荐指数
1
解决办法
133
查看次数

尝试在上传前预览图像

我正在尝试渲染用户在页面上输入文件类型的图像,因此用户将在提交和上传图像之前预览图像,但我无法获取其值。

我一直试图做的是使用输入的值作为<img />组件的来源:

import React, { useState } from "react";

export default function Test() {
  const [image, setImage] = useState(null);
  return (
    <div>
   <form  onSubmit={() => {
      //Sumbit
    }}/>
    <input
      type="file"
      onChange={(e) => setImage(e.target.value)}
    ></input>
    {image ? <img src={image} /> : null}
    <button type={"submit"}></button>
  </form>
    </div>
  );
}
Run Code Online (Sandbox Code Playgroud)

但它总是返回一个值 - C:\fakepath\IMGName。有什么方法可以获取图像的值,以便将其渲染为图像?

html reactjs

0
推荐指数
1
解决办法
32
查看次数

标签 统计

reactjs ×3

javascript ×2

angular ×1

express ×1

html ×1

node.js ×1

nodes ×1

react-router ×1