【问题标题】:Axios without a full URL没有完整 URL 的 axios
【发布时间】:2021-03-07 23:37:53
【问题描述】:

我在域中有一个网页应用程序:sxxxcex-cxxfc.web.app

我将从其使用 API 的服务器位于 xx.x2.xx.74:8000

要使用 Axios 登录,我执行以下操作:

 const url='xx.x2.xx.74:8000/login/'

axios.get(url+user+"/"+password)
 .then((res: { data: any; }) => {
     setIngresar(false);
     const resquest = res.data;
     if(resquest=="invalid user"){
       console.log(resquest);
     }
     else{
        setItem("isRegistered", resquest[0].user);
     }
  }).catch((err: any) => {
    console.log(err);
    setIngresar(false)
  })

GET方法的完整路径是

https://sxxxcex-cxxfc.web.app/xx.x2.xx.74:8000/login/theUSer/ThePassword

但应该是:

https://xx.x2.xx.74:8000/login/theUSer/ThePassword

有人能告诉我如何在没有 sxxxcex-cxxfc.web.app 的情况下发送最后一个网址吗?

【问题讨论】:

  • 你试过这样吗? const url = 'https://xx.x2.xx.74: 8000/login';然后是你的代码。
  • 是的,我做到了。我试过了
  • 那不行吗?
  • https://sxxxcex-cxxfc.web.app/xx.x2.xx.74:8000/login/theUSer/ThePassword是axios想出来的相对路径,因为'xx.x2.xx.74:8000/login/'之前没有https://
  • 嗨@codemonkey。我知道。但它会将请求发送到另一个域。

标签: javascript reactjs typescript axios


【解决方案1】:

这是因为您没有在网址中添加“https://”。 所以当你通过 axios 发送请求时,它会尝试在同一个域中查找 url。 如果您在本地尝试您的代码,则 url 应该是 https://localhost:3000/xx.x2.xx.74.8000/login/theUSer/ThePassword。 所以你应该像这样在url前面添加“https://”。

url = 'https://xx.x2.xx.74: 8000/login';
axios.get(url+user+"/"+password)
 .then((res: { data: any; }) => {
     setIngresar(false);
     const resquest = res.data;
     if(resquest=="invalid user"){
       console.log(resquest);
     }
     else{
        setItem("isRegistered", resquest[0].user);
     }
  }).catch((err: any) => {
    console.log(err);
    setIngresar(false)
  })

【讨论】:

  • 谢谢。可以是http吗?而不是 https?
  • 是的。这取决于你的 api。
  • 就javascript而言,您可以使用http或https。但是,Web 浏览器越来越多地需要 https 来让您的应用程序被认为是安全和可信的。因此,您可能会看到 javascript 发出的 http 请求被 Web 浏览器阻止。
  • 有可能作为应用程序,即 Firebase 域是 https 放置 http 时,我通过预先添加 https 将 axios 添加到服务器域?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多