为什么我无法通过axios从API端点获取回复?

huangapple go评论58阅读模式
英文:

Why am I unable to get a reply from the api endpoint with axios

问题

I am sending a request to an endpoint but it doesn't return a response. The endpoint is available. Here is my code:

import './App.css';
import React, { useState, useEffect } from 'react';
import Dashboard from './Dashboard';
import Login from './login/Login';
import axios from 'axios';

export default function App() {
  const [isLogged, setIsLogged] = useState(false);
  const [csrf, setCsrf] = useState("");

  const url = 'https://mtstorez.000webhostapp.com/app/store/welcome';

  // Get login status of the user
  useEffect(() => {
    axios.post(url, {
      key1: 'value1',
      key2: 'value2'
    })
    .then(response => {
      console.log('AJAX request successful');
      // setData(response.data);
    })
    .catch(error => {
      console.log('AJAX request failed');
      // setError(error.message);
    });
  }, []);

  return (
    <main>
      {isLogged ? <div> <Dashboard/> </div> : <Login/>}
    </main>
  )
}

I tried the jsonplaceholder endpoint to generate fake data, and it worked. I also allowed requests from all origins, but it didn't work. Thanks for your help in advance.

英文:

I am sending a request to an endpoint but it doesn't return a response. The endpoint is available.

Here is my code:


import &#39;./App.css&#39;
import React,{useState,useEffect} from &#39;react&#39;
import Dashboard from &#39;./Dashboard&#39;
import Login from &#39;./login/Login&#39;
import axios from &#39;axios&#39;



export default function App() {
  
const[isLogged,setIsLogged] = useState(false);


  const[csrf,setCsrf] = useState(&quot;&quot;);

  const url = &#39;https://mtstorez.000webhostapp.com/app/store/welcome&#39;

  
  //get login status of user
  useEffect(() =&gt; {
    axios.post(url, {
      key1: &#39;value1&#39;,
      key2: &#39;value2&#39;
    })
    .then(response =&gt; {
      console.log(&#39;AJAX request successful&#39;);
      //setData(response.data);
    })
    .catch(error =&gt; {
      console.log(&#39;AJAX request failed&#39;);
      //setError(error.message);
    });
  }, []);



  
  return (

  
    &lt;main&gt;

      {
        isLogged ? &lt;div&gt; &lt;Dashboard/&gt; &lt;/div&gt; :
      &lt;Login/&gt;
      }
    &lt;/main&gt;
  )
}


I tried jsonplaceholder endpoint to generate fake data and it worked. I also allowed request from all origin and it didn't work also.

Thanks for your help in advance

答案1

得分: 1

我已经尝试过您的端点,使用Postman进行POST和GET请求,都获得了正确的响应。

然而,在浏览器中尝试时,只有GET请求按预期执行。我已经设置了一个可重现的示例,链接在这里:https://github.com/jbrocher/question-76220440。POST版本出现了预检错误。

Postman和浏览器之间的行为差异指向了一个CORS(跨源资源共享)问题。

根据我在Postman中进行的测试,端点似乎在使用GET和POST时返回相同的内容,因此我建议像我在链接的存储库中所做的那样使用GET版本。如果您对端点有控制权,也应该验证您的CORS设置。

英文:

I've tried your endpoint using postman and got back a proper response both with POST and GET.

However, when tried from a browser, only the GET request performs as expected. I've set up a reproductible example here :https://github.com/jbrocher/question-76220440. The POST version fails with a preflight error.

The difference of behavior between postman and the browser points to a CORS issue.

From the test I've done with Postman, it would seem the endpoint returns the same thing both using GET and POST so I would suggest using the GET version as I did in the repository I linked. I you have control over the endpoint, should verify your CORS settings as well.

huangapple
  • 本文由 发表于 2023年5月10日 23:51:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定