axios 500(内部服务器错误)

new9mtju  于 2022-11-05  发布在  iOS
关注(0)|答案(2)|浏览(435)

我尝试在React中使用axios进行API调用。但我不知道为什么我总是得到错误500。当我检查API并通过URL输入时,它工作并给予JSON响应,但在reactJS中它不工作并返回内部服务器错误500。有人能帮助我吗?
package.json

"proxy": "https://myAPI.net",

Register.js

const handleRegister = async () =>{
      let error = false;
      validateEmail();
      comparePwd();

      if(isPwdMatches === false || isEmailInvalid === true){
        error = true
      }

      if(error === false){
        console.log(`test`)
        try{
          const response = await axios.post('/registerUser', {
            password: values.password,
            email: values.email,
            name: values.name,
          });
          console.log(response)
          const data = await response.data.data;
          console.log(data)
          setRegistered(true);
        } catch(e){
          console.log(e)
        }
      }
    };

错误消息:

POST http://localhost:3000/registerUser 500 (Internal Server Error)

Register.js:98 Error: Request failed with status code 500
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:62)

预期https://myAPI.net/registerUser?password=myPassword&email=myEmail@gmail.com&name=myName的输出

{
token: {
kind: "identitytoolkit#VerifyPasswordResponse",
email: "myEmail@gmail.com",
displayName: "myName",
idToken: "generatedToken",
registered: true
},
message: "successfully created user profile"
}

有谁能帮我解决这个问题吗?我是一个很新的React和使用axios,所以任何帮助都是很好的!提前感谢!

k97glaaz

k97glaaz1#

它看起来像你必须发送get请求到你的api端点,因为你提到了https://myAPI.net/registerUser?password=myPassword&email=myEmail@gmail.com&name=myName`所有的密码电子邮件和...是arm参数,你必须把它们作为参数传递

const result = await axios.get("yourApiAddress", {params : {email : "",password: "", email : ""})

请注意,这是可能的,你必须张贴数据。如果这种方法不工作,试试这个。

const result = await axios.post("yourApiAddress",{}, {params : {email : "",password: "", email : ""})

希望你能做到这一点:)
祝你好运!

nfeuvbwi

nfeuvbwi2#

确保服务器已启动并正在运行。

相关问题