Javascript GET或HEAD请求不允许可能的未处理承诺拒绝(id 0)typeerror正文 从“React”导入React; 从“react native”导入{FlatList,ActivityIndicator,Text,View}; 导出默认类FetchExample扩展React.Component{ 建造师(道具){ 超级(道具); this.state={isLoading:true} } componentDidMount(){ 取('https://apitest.kuveytturk.com.tr/prep/v1/data/fecs', { 方法:“GET”, 标题:{ 接受:'application/json', “内容类型”:“应用程序/json”, }, 正文:JSON.stringify({ firstParam:'isoCode', 第二个参数:“国际代码”, 第三个参数:“名称”, 第四个参数:“代码”, 第五个参数:“组”, 第六个参数:“id” }), }); } render(){ if(此.state.isLoading){ 返回( ) } 返回( {item.isoCode}、{item.internationalCode}、{item.name}、{item.code}、{item.group} keyExtractor={({id},索引)=>id} /> ); } }
我是react native的初学者,我正在尝试从api获取数据,但我在代码方面有一个错误 可能未处理的承诺拒绝(id 0)typeerror undefined不是对象 我不明白为什么没有目标Javascript GET或HEAD请求不允许可能的未处理承诺拒绝(id 0)typeerror正文 从“React”导入React; 从“react native”导入{FlatList,ActivityIndicator,Text,View}; 导出默认类FetchExample扩展React.Component{ 建造师(道具){ 超级(道具); this.state={isLoading:true} } componentDidMount(){ 取('https://apitest.kuveytturk.com.tr/prep/v1/data/fecs', { 方法:“GET”, 标题:{ 接受:'application/json', “内容类型”:“应用程序/json”, }, 正文:JSON.stringify({ firstParam:'isoCode', 第二个参数:“国际代码”, 第三个参数:“名称”, 第四个参数:“代码”, 第五个参数:“组”, 第六个参数:“id” }), }); } render(){ if(此.state.isLoading){ 返回( ) } 返回( {item.isoCode}、{item.internationalCode}、{item.name}、{item.code}、{item.group} keyExtractor={({id},索引)=>id} /> ); } },javascript,android,react-native,Javascript,Android,React Native,我是react native的初学者,我正在尝试从api获取数据,但我在代码方面有一个错误 可能未处理的承诺拒绝(id 0)typeerror undefined不是对象 我不明白为什么没有目标 希望您的帮助。请注意,您不能在GET请求中使用body。请将其更改为POST。您不能在GET请求中使用body。如果希望通过GET请求传递参数,那么使用查询参数的正式方法就是将它们添加到URL中。这是一个例子: import React from 'react'; import { FlatList,
希望您的帮助。请注意,您不能在GET请求中使用body。请将其更改为POST。您不能在GET请求中使用body。如果希望通过GET请求传递参数,那么使用查询参数的正式方法就是将它们添加到URL中。这是一个例子:
import React from 'react';
import { FlatList, ActivityIndicator, Text, View } from 'react-native';
export default class FetchExample extends React.Component {
constructor(props){
super(props);
this.state ={ isLoading: true}
}
componentDidMount(){
fetch('https://apitest.kuveytturk.com.tr/prep/v1/data/fecs', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'isoCode ',
secondParam: 'internationalCode',
thirdParam: 'name',
fourthParam: 'code',
FifthParam: 'group',
SixthParam: 'id'
}),
});
}
render(){
if(this.state.isLoading){
return(
<View style={{flex: 1, padding: 20}}>
<ActivityIndicator/>
</View>
)
}
return(
<View style={{flex: 1, paddingTop:20}}>
<FlatList
data={this.state.dataSource}
renderItem={({item}) => <Text>{item.isoCode}, {item.internationalCode}, {item.name}, {item.code}, {item.group}</Text>}
keyExtractor={({id}, index) => id}
/>
</View>
);
}
}
你能分享你得到的全部错误吗?我不能添加关于错误的照片,因为声誉问题,但错误是:可能未处理的承诺拒绝(id 0)类型错误正文不允许GET或HEAD请求检查此项你不能在GET请求中添加正文,这就是错误所在
var url = new URL("https://apitest.kuveytturk.com.tr/prep/v1/data/fecs"),
params = {firstParam: 'isoCode ',
secondParam: 'internationalCode',
thirdParam: 'name',
fourthParam: 'code',
FifthParam: 'group',
SixthParam: 'id'}
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
fetch(url).then(/* … */)