- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从 WebAPI 获取了此数据,并希望更新 Web Api 中 imageUri 和 employeeBio 的值。我现在只能编辑 Bio,现在我想发送 PUT 请求来更新 imageUri 和 employeeBio 的值,但无法这样做。我的“保存”按钮的 onPress 方法似乎不起作用,因为按下按钮时我无法收到警报。谢谢!
这是网址中存在的 JSON 数据。
[
{
"imageUrl": "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1200px-React-icon.svg.png",
"departmentName": "Test Department",
"employeeName": "Test Employee",
"employeeBio": "Test Bio"
}
]
这是 DataLoad.js 中配置文件数据的 PUT 方法。由于用户只能更改 Image 和 io,因此我们在调用 PUT 请求时仅接受 imageUrl 和 employeeBio 的值。
export function updateProfileData(params) {
return fetch('http://www.json-generator.com/api/json/get/cgdMFRuLTm?indent=2', {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({params}),
})
.then((response) => response.json())
.then((result) => {
if (result === 'ok') {
alert('Profile Updated Successfully!');
this.setState({
imageUrl: this.state.items[0].imageUrl,
employeeBio: this.state.items[0].employeeBio
})
}
})
.catch((error) => {
alert('Profile Update Failed!')
console.log(`error = ${error}`);
});
}
由于我对 React-Native 还很陌生,因此我不得不浏览多个视频和指南才能使 GET 请求正常工作。这就是我目前在 Profile.js 文件中调用 GET 请求和 PUT 请求的内容。
constructor(props) {
super(props);
this.state = {
items: [],
isLoaded: false,
employeeName: '',
departmentName: '',
employeeBio: '',
imageUrl: ''
};
this.getProfileData = getProfileData.bind(this);
this.updateProfileData = updateProfileData.bind(this)
}
componentDidMount() {
this.getProfileData();
}
render() {
var { isLoaded } = this.state
if (!isLoaded) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator />
</View>
);
} else {
return (
<View style={styles.container}>
<View style={styles.piccontainer}>
<Image
onPress={() => this.props.navigation.navigate('Profile')}
source={{ uri: this.state.items[0].imageUrl }}
style={styles.photo} />
</View>
<View style={styles.textcontainer}>
<View style={styles.listView}>
<Text style={styles.label}>Name </Text>
<Text style={styles.name}>{this.state.items[0].employeeName}</Text>
</View>
<View style={styles.listView}>
<Text style={styles.label}>Department </Text>
<Text style={styles.name}>{this.state.items[0].departmentName}</Text>
</View>
<View style={styles.listView}>
<Text style={styles.label}>Bio </Text>
<TextInput
multiline={true}
numberOfLines={4}
style={styles.input}
value={this.state.items[0].employeeBio}
onChangeText={(text) => this.setState({
items: this.state.items.map((item, i) =>
i == 0 ?
{ ...item, employeeBio: text } : item)
})
} />
</View>
</View>
<View style={{ alignSelf: "center" }}>
<TouchableOpacity style={styles.button}>
<View>
<Text style={styles.text}
onPress={() => {
let params = {
imageUrl: this.state.items[0].imageUrl,
employeeBio: this.state.items[0].employeeBio
};
updateProfileData(params);
}
} >
Save
</Text>
</View>
</TouchableOpacity>
</View>
</View>
)
}
}
已编辑:我现在可以更改该值,但仍然面临调用 PUT 请求和更新 Web API 上的值的问题。似乎我的 保存 按钮中的 onPress 方法无法正常工作,因为我没有收到显示我已成功更新个人资料的警报。再次感谢您的帮助!
最佳答案
首先是你提供的方法updateProfileData
未在其定义中接收任何参数。
其次,您在onChange
中使用扩展运算符的方式事件不是更改数组状态变量的正确方法。您可以在对象中执行此操作,但不能在数组中执行此操作。您需要更新数组中正确的对象,因为您应该使用某种循环机制来获取要更新的对象,因此您应该执行如下操作: this.setState({items: this.state.items.map((item, i) => i==0 ? {...item, bio: text} : item)});
这里 i==0 适用于您的情况。您也可以动态地使用示例。希望这会有所帮助。
关于javascript - 无法发送仅更改 imageUri 和employeeBio 的 PUT 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59793682/
在调试以下代码时,我将 picUri 的值设为 null,并且我可以看到 picUri=null data:"Intent"{act =inline-data (has extras)}" in tr
我期待从 ImageView 访问当前图像的 Uri 的方法,以将其保存到我的数据库中,但我只找到了 setter。 setter/getter 在哪里?? 最佳答案 没有 setter/getter
我使用 PhoneGap 创建移动应用程序,可以从相册中选择图像,然后我想传递该图像并显示在另一个 html 页面中。有人知道该怎么做吗?这是我的代码 选择图像.html
我想从我的画廊加载图片并将其显示在我的应用程序中。我尝试了 ImageBitmap 和 ImageURI 但我只是不显示图片。 这是我的java代码的一部分: private ListView LIS
因此,有两个问题与此相同( How to use getCropAndSetWallpaperIntent method in WallpaperManager?和 How to use getCro
因此,有两个问题与此相同( How to use getCropAndSetWallpaperIntent method in WallpaperManager?和 How to use getCro
我从 WebAPI 获取了此数据,并希望更新 Web Api 中 imageUri 和 employeeBio 的值。我现在只能编辑 Bio,现在我想发送 PUT 请求来更新 imageUri 和 e
我正在尝试使用带有附件电话间隙插件的电子邮件编辑器从我的电话间隙应用程序发送图片作为附件。但是我无法让它工作。 我尝试了两种不同的方法。第一种方法是使用 Phonegap 相机 api 将 image
我需要通过在 intent extra 中提供的路径上创建一个图像文件来 stub 相机 intent。Espresso 只能响应 activityresult。我在哪里可以执行操作以从 intent
我正在尝试从手机相册中选取的图像中获取 base64,但我无法使其工作: 我试过这个: window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
我在旧版本中使用 getBitmap 方法,但我找不到 getBitmap From Uri 的任何替代方法。 try { bitmap = MediaStor
这肯定是一个真正的新手问题,可能比 PhoneGap 更多地涉及到 Javascript 的工作原理。我正在关注this video它只是使用 PhoneGap 的相机插件拍照。 Here is相机插
我目前正在使用下面的函数从照片库中获取图像 function getImage() { // Retrieve image file location from specified sourc
我正在使用 CLoudFormation 在 AWS 上构建基础设施,并且想要创建一个从 ECR 提取代码镜像的 Lambda 函数。因此,我根据 ECR 存储库资源向我的 CloudFormatio
我已经被这行代码困住了好几天了。我有一个 ImageButton,它启动相机 Intent ,保存照片,将照片的位图缩略图设置到 ImageButton。我最终希望这张照片显示在 Google map
出于某种原因,我无法弄清楚为什么会出现问题。 我已经创建了一个公共(public) ECR 存储库,并尝试在我的 AWS::Lambda::Function 中的 Code block 下将图像引用为
我是一名优秀的程序员,十分优秀!