axios 错误:请求失败,状态代码为400,在POSTMAN中发送和在应用程序中发送之间的差异

2admgd59  于 2023-08-04  发布在  iOS
关注(0)|答案(4)|浏览(95)

在Postman中,它可以工作:

1) url: https://app/login
2) method: POST
3) body
4) x-www-form-urlencoded
5)username: ****,
password: ****,
grant_type: 'password',
client_secret: '****',
client_id: '****'

字符串
在函数submit方法POST中,当表单提交时,它不工作。我有错误:
xhr.js?b50d POST https://app/login 400(Bad Request)
错误:请求失败,在createError(createError.js?2d 83)在settle(settle.js?467f)在XMLHttpRequest.handleLoad(xhr.js?b50d)
response中的选项卡Network中,我有:
{“error”:“invalid_client”,“error_description”:“在标头或正文中未找到客户端凭据”}

登录

class Login extends Component {
  constructor (props) {
    super(props);
    this.state = {
      email: '',
      password: ''
    }
  }

  handle = (e) => {
    const name = e.target.name;
    const value = e.target.value;
    this.setState({
      [name]: value
    });
  }

  submit = (event) => {
    event.preventDefault();

    const body1 = {
      username: this.state.email,
      password: this.state.password,
      grant_type: 'password',
      client_secret: '****',
      client_id: '****'
    }

    axios({ 
      method: 'post', 
      url: 'https://app/login', 
      body: body1, 
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
      }) 
      .then(res => { 
        if (res.status === 200) {
          console.log(res)
        } 
      }).catch(err => { 
        console.error(err);
      });

  }

  render () {
    return (
      <form action="" method="post" onSubmit={this.submit}>
        <div>
          <label htmlFor="email">Email</label>
          <input type="email" required  name="email"
            value={this.state.email}
            onChange={this.handle}  />
        </div>
        <div>
          <label htmlFor="password">Password</label>
          <input type="password"name="password"
            value={this.state.password}
            onChange={this.handle}  />
        </div>
        <button type="submit" value="Submit">Log</button>
      </form>
    )
  }
}


在POSTMAN中发送和在应用程序中发送有什么区别?正文的内容转换为字符串?

gzjq41n4

gzjq41n41#

Axios处理错误的方式不同。
找出真正的问题所在。
你应该使用error.request来检查你的请求是否有错误
并使用error.response从服务器获取错误反馈

axios({ 
      method: 'post', 
      url: 'https://app/login', 
      body: body1, 
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
      }) 
      .then(res => { 
        if (res.status === 200) {
          console.log(res)
        } 
      }).catch(err => { 
          if(err.request){
              console.log(err.request)
          }
          if(err.response){
              console.log(err.response)
          }
      });

字符串

dxxyhpgq

dxxyhpgq2#

代码在Content-Type中声明主体将被URL字符串编码,但在主体中它被赋予了一个JavaScript对象。看起来Axios客户端并没有将body对象转换为URL编码的值(即从{a: 5, b: 2}"a=5&b=2")。该代码需要一个函数转换.最常见的是qs
否则你的数据可能会被转换成一个字符串与.toString()方法,这将给予你"[object Object]",你应该能够看到这在网络标签的开发工具。

exdqitrt

exdqitrt3#

Localhost:3000/API/products 404错误您没有在server.js上创建res.get(“/api/products”)或者没有设置代理。检查下面的代理设置。
代理错误:could not proxy request /API/products检查此:

  1. frontend/package.json
    {“name”:“frontend”,“proxy”:“http://127.0.0.1:5000“,…}
    1.停止运行前端和后端
    1.先运行后端
    npm启动
    1.然后前端
    cd frontend npm start
whlutmcx

whlutmcx4#

代码在Content-Type中声明主体将被URL字符串编码,但在主体中它被赋予了一个JavaScript对象。看起来Axios客户端并没有将body对象转换为url编码的值(即from {a:5,B:2}到“a=5&b=2”)。该代码需要一个函数转换.一个流行的是qs。

npm i qs;
import qs as qs;

axios.post(your_url,
  qs.strigify({
      key1:value1,
      key2:value2,
 }),{
headers:{
   'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
})

字符串

相关问题