Json Postman给出响应,但ReactJS代码抛出错误
我试图从API中获取记录,但当我使用下面的代码时,它返回不成功的响应。但同时,它给了我一个恰当的回应,邮递员的状态代码是200。这是我的密码。请帮助:Json Postman给出响应,但ReactJS代码抛出错误,json,reactjs,api,fetch,fetch-api,Json,Reactjs,Api,Fetch,Fetch Api,我试图从API中获取记录,但当我使用下面的代码时,它返回不成功的响应。但同时,它给了我一个恰当的回应,邮递员的状态代码是200。这是我的密码。请帮助: import React, {Component} from "react"; const urlEndPoint = myEndPoint => "https://api.github.com/users/adhishlal" export default class Categories extends Compon
import React, {Component} from "react";
const urlEndPoint = myEndPoint => "https://api.github.com/users/adhishlal"
export default class Categories extends Component {
constructor(props) {
super(props)
this.state = {
requestFailed: false
}
}
componentDidMount() {
fetch(urlEndPoint(this.props.myEndPoint))
.then(response => {
if (!response.ok) {
throw Error(response.statusText)
}
return response
})
.then(d => response.json())
.then(response => {
this.setState({
responseData: d
})
}, () =>
{
this.setState({
requestFailed: true
})
})
}
render() {
//If request is failed
if(this.state.requestFailed)
{
return <div>Request Failed</div>
}
//Waiting for data to load from Zoho
if(!this.state.responseData)
{
return <div>Loading...</div>
}
//Finally populate the data when loaded
var indents = [];
for (var i = 0; i < 10; i++) {
indents.push(
<div className="col-sm-4 col-xs-12"><div className="cr-openings">
<p className="no-margin font-color-lightGrey no-of-openings">{i+1}</p><p className="catg-openings font-color-white">{this.state.responseData.name}</p>
<p className="font-color-lightGrey city-openings no-margin">Bangalore , Chennai , Delhi NCR , Hyderabad , Mumbai</p>
</div>
</div>
);
}
return (
<section className="dotted">
<div className="dotted-bg-dark">
<div className="container padding-80-0">
<div className="row m-b-30">
{indents}
</div>
</div>
</div>
</section>
);
}
}
import React,{Component}来自“React”;
常量urlEndPoint=myEndPoint=>“https://api.github.com/users/adhishlal"
导出默认类类别扩展组件{
建造师(道具){
超级(道具)
此.state={
请求失败:false
}
}
componentDidMount(){
获取(urlEndPoint(this.props.myEndPoint))
。然后(响应=>{
如果(!response.ok){
抛出错误(response.statusText)
}
返回响应
})
.then(d=>response.json())
。然后(响应=>{
这是我的国家({
答复数据:d
})
}, () =>
{
这是我的国家({
请求失败:true
})
})
}
render(){
//如果请求失败
if(this.state.requestFailed)
{
返回请求失败
}
//正在等待从Zoho加载数据
如果(!this.state.responseData)
{
返回加载。。。
}
//最后在加载时填充数据
var缩进=[];
对于(变量i=0;i<10;i++){
压入(
{i+1}
{this.state.responseData.name}
班加罗尔、钦奈、新德里、海得拉巴、孟买
);
}
返回(
{indents}
);
}
}
你能告诉我哪里出了错吗?您可以在restclient或postman中运行API以查看响应。我还没有完成您的代码,但将向您展示如何使用fetch获取数据
fetch('https://api.github.com/users/adhishlal')
.then(response => {
response.json().then(function(data){
console.log(data);
})
代码中的数据将是您需要的数据
fetch()请求的响应是一个Stream对象,这意味着当我们调用json()方法时,将返回一个承诺,因为流的读取将异步进行
在我看来,改用英语将使生活更轻松
希望这有帮助我还没有完成您的代码,但将向您展示如何使用fetch获取数据
fetch('https://api.github.com/users/adhishlal')
.then(response => {
response.json().then(function(data){
console.log(data);
})
代码中的数据将是您需要的数据
fetch()请求的响应是一个Stream对象,这意味着当我们调用json()方法时,将返回一个承诺,因为流的读取将异步进行
在我看来,改用英语将使生活更轻松
希望这有帮助上面编写的代码几乎没有错误。在某些位置返回了不正确的值。同样,setState是为错误的变量执行的。修好这些会让它运转良好
我已经更新了导致问题的代码
.then(d => d.json()) //Return d.json() and not response.json()
.then(response => {
this.setState({
responseData: response, //setState to response and not d
})
}, () =>
上面编写的代码几乎没有错误。在某些位置返回了不正确的值。同样,setState是为错误的变量执行的。修好这些会让它运转良好
我已经更新了导致问题的代码
.then(d => d.json()) //Return d.json() and not response.json()
.then(response => {
this.setState({
responseData: response, //setState to response and not d
})
}, () =>
听起来像是CORS的问题。您的端点url是否启用了CORS?@Souradeep Nanda也检查了CORS。这没用。听起来像是CORS的问题。您的端点url是否启用了CORS?@Souradeep Nanda也检查了CORS。这没有帮助。