我正在玩React(我是新手)并尝试向我的rails API发送帖子请求(在localhost atm上)。我将post函数编写为console.log我的响应,我一直得到200响应,确认该对象已在我的API中成功创建。但是,我想查看我的http响应,以确认已经传递了正确的参数,似乎没有正文。不确定我的帖子请求中是否遗漏了,或者在我发布帖子请求后我是否错误地更新了我的状态。
我的反应组件
import React, { Component } from 'react';
import axios from 'axios'
import update from 'immutability-helper'
import BusinessForm from './businessForm'
const API = 'http://localhost:3001/api/v1/businesses/'
class NewBusinesses extends Component {
state = {
businesses: [],
editID: null
}
check = () => {
console.log(this.state.businesses)
}
addNew = () => {
axios.post(
API,
{
business:
{ name: 'NEW BUSINESS',
address: '',
city: '',
zip: '',
wifi: '',
phone: '',
bathroom: ''
}
}
)
.then(response => {
console.log(response)
const businesses = update(this.state.businesses, {
$splice: [[0,0,response.data]]
})
this.setState({businesses: businesses,
editID: response.data.id})
})
.catch(error => console.log(error))
}
我点击该功能后的控制台(console.log(响应))
{data: "", status: 200, statusText: "OK", headers: {…}, config: {…}, …}
config:{adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}
data:""
headers:
{content-type: "text/html", cache-control: "no-cache"}
request
:
XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status:200
statusText:"OK"
__proto__:Object
答案 0 :(得分:0)
没关系!这个问题出在API方面,不是因为我在React应用程序中做了什么。我不得不添加一行
render json:@business
到我的Rails API中的create方法