反应本机Post错误';无法加载空URL';
尝试使用下面的代码在React中发出POST请求。为了维护隐私,我删除了url、accesskey和varArgs变量的实际参数并插入了伪参数。当我尝试启动我收到的请求时-'错误:无法加载空url'。我的url变量是一个有效字符串,post请求在curl中可以正常工作。有人能看出我做错了什么吗反应本机Post错误';无法加载空URL';,post,react-native,Post,React Native,尝试使用下面的代码在React中发出POST请求。为了维护隐私,我删除了url、accesskey和varArgs变量的实际参数并插入了伪参数。当我尝试启动我收到的请求时-'错误:无法加载空url'。我的url变量是一个有效字符串,post请求在curl中可以正常工作。有人能看出我做错了什么吗 import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Touc
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
Picker
} from 'react-native';
var vsprintf = require('sprintf-js').vsprintf
class plantMetrics extends Component {
constructor(props){
super(props)
const url = 'http://myurl.com'
const accessKey = 'myaccesskey'
const varArgs = '{"arg1":"val1"}'
this.state = {
catalogMeta: 'shit',
workspace: '',
measure: '',
bu: '',
country: '',
plant: '',
region: ''
}
}
getCatalogInfo(){
fetch(this.url, {
method: "POST",
headers: {
'Content-Type': 'application/json',
'Authorization': this.accessKey
},
body: this.varArgs
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({catalogMeta: JSON.stringify(responseJson.body)})
})
.catch((error) => {
this.setState({catalogMeta: 'error: ' + error})
})
}
render() {
return (
<View>
<Text>{"\n"}</Text>
<Text>{this.state.catalogMeta}</Text>
<TouchableHighlight onPress={this.getCatalogInfo.bind(this)}>
<Text>Fetch</Text>
</TouchableHighlight>
</View>
);
}
import React,{Component}来自'React';
进口{
评估学,
样式表,
文本,
看法
触控高光,
采摘者
}从“反应本机”;
var vsprintf=require('sprintf-js')。vsprintf
类plantMetrics扩展组件{
建造师(道具){
超级(道具)
常量url=http://myurl.com'
const accessKey='myaccesskey'
常量varArgs='{“arg1”:“val1”}
此.state={
他妈的,
工作区:“”,
措施:'',
布:“,
国家:“,
植物:'',
区域:“”
}
}
getCatalogInfo(){
获取(this.url{
方法:“张贴”,
标题:{
“内容类型”:“应用程序/json”,
“授权”:this.accessKey
},
body:这是varArgs
})
.then((response)=>response.json())
.然后((responseJson)=>{
this.setState({catalogMeta:JSON.stringify(responseJson.body)})
})
.catch((错误)=>{
this.setState({catalogMeta:'error:'+error})
})
}
render(){
返回(
{“\n”}
{this.state.catalogMeta}
取来
);
}
下面的变量
const url = 'http://myurl.com'
const accessKey = 'myaccesskey'
const varArgs = '{"arg1":"val1"}'
仅在构造函数
方法的范围内。要通过此
访问它们,请使用此
设置它们:
this.url = 'http://myurl.com'
this.accessKey = 'myaccesskey'
this.varArgs = '{"arg1":"val1"}'