[英]Get axios call in reactjs
Below is the response I am getting after doing get call:以下是我在拨打电话后得到的回复:
{
"id": “12345”,
“details”: {
“name”: “sample doc”,
“market: “sample market”
}
}
My Service Method:我的服务方式:
ENDPOINTS = {
product: "/market/product",
}
getDetails(
id: string
): Promise<{
id: string;
}> {
const url = `${this.ENDPOINTS.PRODUCT}/${id}/name`;
return http
.get(url)
.then((response) => {
return response.data;
})
.catch((error) => {
throw error;
});
}
My component Method:我的组件方法:
getTestingDone = () => {
this.sampleService
.getDetails(
this.props.product.id,
)
.then((response) => {
this.setState({
});
})
.catch((error) => {
console.log(error);
});
};
<TextInput
labelText="name"
type="text"
name="keyname"
value = {name}
/>
I want to print the response in this input field.我想在这个输入字段中打印响应。 Not sure How to get the response from the server to the UI.
不确定如何从服务器获得对 UI 的响应。 Can anyone help me with this.Do i need to make a model class?
谁能帮我解决这个问题。我需要制作 model class 吗? and return it in response in service method?
并在服务方法中返回它作为响应?
constructor() {
this.state = {
name: ''
}
}
getTestingDone = () => {
this.sampleService
.getDetails(this.props.product.id)
.then(({details: {name}}) => {
this.setState(state => ({...state, name}));
})
}
render() {
const { name } = this.state;
return <TextInput
labelText="name"
type="text"
name="keyname"
value = {name}/>
}
This is the correct answer for the above question:这是上述问题的正确答案:
constructor(){
this.state = {
details: {
name: "",
market: "",
}
}
getTestingDone = () => {
this.sampleService
.getDetails(this.props.product.id)
then((response) => {
this.setState({
credentials: response.credentials,
});
})
return <TextInput
labelText="name"
type="text"
name="keyname"
value = {this.state.details.name}/>
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.