亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤:請求失敗,狀態碼為 400。在 POSTMAN 和應用程序中發送之間的差異

錯誤:請求失敗,狀態碼為 400。在 POSTMAN 和應用程序中發送之間的差異

喵喵時光機 2021-08-20 15:29:47
在郵遞員它工作:1) url: https://app/login2) method: POST3) body4) x-www-form-urlencoded5)username: ****,password: ****,grant_type: 'password',client_secret: '****',client_id: '****'在函數submit方法POST中,當表單提交時,它不起作用。我有錯誤:xhr.js?b50d POST https://app/login 400(錯誤請求)錯誤:在 XMLHttpRequest.handleLoad (xhr.js?b50d) 處的 createError (createError.js?2d83) 處,請求失敗,狀態碼為 400 (settle.js?467f)在選項卡Network中response我有:{"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 中發送和在應用程序中發送有什么區別?正文內容轉換為字符串?
查看完整描述

3 回答

?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

代碼在 Content-Type 中聲明主體將是 URL 字符串編碼的,但在主體中它被賦予了一個 JavaScript 對象。似乎 Axios 客戶端不會將該 body 對象轉換為 url 編碼值(即 from?{a: 5, b: 2}to?"a=5&b=2")。代碼需要一個函數來轉換它。一個流行的是qs。

否則,您的數據可能會被轉換為字符串,該.toString()方法將為您提供"[object Object]",您應該能夠在開發人員工具的網絡選項卡中看到這一點。


查看完整回答
反對 回復 2021-08-20
?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

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) } });


查看完整回答
反對 回復 2021-08-20
?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

Localhost:3000/api/products 404 錯誤 您沒有在 server.js 上創建 res.get("/api/products") 或者您沒有設置代理。檢查下面的代理設置。

代理錯誤:無法代理請求 /api/products 檢查:

  1. 前端/package.json

    { "name": "frontend", "proxy": "http://127.0.0.1:5000", ... }

  2. 停止運行前端和后端

  3. 先運行后端

    啟動

  4. 然后前端

    cd 前端 npm start


查看完整回答
反對 回復 2021-08-20
  • 3 回答
  • 0 關注
  • 473 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號