首页 > 解决方案 > 将请求中的 Nodejs 请求转换为 React-native

问题描述

我需要转换以下请求,即 react-native 的以下代码:

节点:

const request = require('request');

var areaRiservata = {
    url: "https://www.iliad.it/account/",
    method: "POST",
    followAllRedirects: true,
    jar: true,
    form: {
        "login-ident": config.iliad.username,
        "login-pwd": config.iliad.password
    }
}

exports.Login = function(callback) {
  request.post(areaRiservata, function (err,res,body) {
    if (String(body).match(/ID utente o password non corretto./g) != null) {
      //Login fallito, restituisco errore
      callback('Errore durante il login. ID utente o password non corretto.');
    } else {
      //Login effettuato, restituisco il sorgente della pagina
      callback(body.replace(/<\/span> \//g, ' /').replace(/GB<br>/g, 'GB</span>'));
    }
  });
};

我已经尝试过了,但它似乎不起作用。

反应原生

fetch('https://www.iliad.it/account/', {
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: JSON.stringify({
        'login-ident': ident,
        'login-pwd': pwd,
      }),
    })
      .then(response => response.text())

例如 fetch 不包含这些字段,就像我可以做的那样。

followAllRedirects: true,
jar: true,

我在哪里做错了?

标签: javascriptnode.jsreact-nativerequestfetch

解决方案


推荐阅读