4Manuals

  • PDF Cloud HOME

为什么我的前端(反应/下一步)没有从我的快递服务器接收查询字符串参数? Download

    反应:更改方向后触发重新渲染和offsetWidth的重新计算 如何在react图表上的datakey中操作DateTime 如何在nextjs中使用html2canvas 使用一些Typescript文件测试React应用 React-native动画时间序列及其百分比或替代 谁能帮助我通过wordpress登录和注册来创建React Native应用? 如何检查电子屏幕截图的允许状态(macOS catalina) 将用户直接添加到/ etc / shadow并期望它能工作,是否可以这样工作? 如何在reactjs中更新值 时间戳格式不支持将React`react-datepicker`作为默认值

我试图阻止从未注册过我的应用程序的用户从数据库中不存在的表单中获取反馈,因此必须注册。

我正在使用fetch与服务器通信。并专门使用passport.js处理身份验证。

在服务器上记录来自端点的响应。

type: "basic"
url: "http://localhost:8016/login?error=true"
redirected: true
status: 200
ok: true
statusText: "OK"

起初我脸红,我认为以下情况适用:

  

从fetch()返回的Promise不会因HTTP错误状态而拒绝   即使响应是HTTP 404或500,它也会解析   通常(将ok状态设置为false),并且只会在   网络故障或是否有任何阻止请求完成的事情。

但是我注意到我的ok状态为true,尽管url属性包含正确的查询字符串参数,但有错误,即error=true

因此,我认为我可以轻松创建一个函数来读取响应对象的url属性并检查query-string属性是否存在错误。

但这是正确的方法吗?

这是我在快递服务器上的路线:

router.route('/login')
    .post(passport.authenticate('local', {
        successRedirect: '/profile',
        failureRedirect: '/login?error=true',
    }))
    .get((req, res) => res.status(404).send({ error: req.query.error }));

这在我的句柄中的fetch调用以我的形式在我的react组件中提交:

    return window.fetch('http://localhost:8016/users/login', {
      method: 'POST',
      headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },
    body: JSON.stringify({ username: username, password: password })
   })
   .then((response)=>{
     console.log('response', response);
      if (response.ok) {
       const { token } = response.clone();
       const loginOptions = {
        token,
        cookieOptions: { expires: 1 },
        callback: () => history.push('/profile')
       }
        setTimeout(() => {
          this.props.logInUser()
         login(loginOptions)
        }, 5000)
         this.setState({
          username: '', password: '', formError: false, formSuccess: true, isLoading:false
         })
         return response.json();
      } else if (response.url.split('?').filter(function(el){return !el.indexOf('error')}).includes('error=true')) {
         this.setState({
          formError: true, formSuccess: false, isLoading: false
         });
         return;
        }
     })
    .catch(function (error) {
       console.log(error);
    });

最后,浏览器中的网址不应为http://localhost:8016/login?error=true 预先感谢您的帮助!

更新

我还注意到控制台中的以下内容:

SyntaxError: Unexpected token < in JSON at position 0

0 个答案:

没有答案



Similar searches
    Samsung RB195 Refrigerator User Manual Android Studio无法识别Chrombook设备,无法在其上安装 如何实现UICollectionView-XAMARIN.IOS 清除输入类型=“日期”时如何捕获? 删除嵌套数组中的空对象