[英]How to use fetch with wordpress rest API
I have this code and I wanna login using the link in the fetch of this below code but I'm getting this error while clicking on the login button: JSON Parse Error: Unexpected EOF. 我有此代码,我想使用以下代码的获取中的链接登录,但单击登录按钮时出现此错误:JSON解析错误:意外的EOF。 I'm trying to use rest API for this login app.
我正在尝试为此登录应用程序使用rest API。
var React = require('react');
var ReactNative = require('react-native');
var t = require('tcomb-form-native');
var {
AppRegistry,
AsyncStorage,
StyleSheet,
Text,
View,
TouchableHighlight,
AlertIOS,
Image,
} = ReactNative;
var STORAGE_KEY = 'id_token';
var Form = t.form.Form;
var Person = t.struct({
username: t.String,
password: t.String
});
const options = {};
var AwesomeProject = React.createClass({
async _onValueChange(item, selectedValue) {
try {
await AsyncStorage.setItem(item, selectedValue);
} catch (error) {
console.log('AsyncStorage error: ' + error.message);
}
},
componentDidMount: function() {
this._userLogin();
},
_userLogin: function() {
var value = this.refs.form.getValue();
if (value) { // if validation fails, value will be null
fetch("http://vowelserver.com/trades/test_autheticate/", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: value.username,
password: value.password,
})
}).then((response) => response.json())
.then((responseData) => {
this._onValueChange(STORAGE_KEY, responseData.id_token)
}).done();
}
},
render() {
return (
<View style={styles.container}>
<View style={styles.container}>
<Image
resizeMode="contain"
source={{uri: 'http://res.cloudinary.com/vowelweb/image/upload/v1498114115/Logo_mqzlgt.png'}}
style={styles.image}
/>
</View>
<View style={styles.row}>
<Form
ref="form"
type={Person}
options={options}
/>
</View>
<View style={styles.row}>
<TouchableHighlight style={styles.button} onPress={this._userLogin} underlayColor='#99d9f4'>
<Text style={styles.buttonText}>Login</Text>
</TouchableHighlight>
</View>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
justifyContent: 'center',
marginTop: 50,
padding: 20,
backgroundColor: '#ffffff',
},
containers: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
backgroundColor: 'white',
position: 'relative',
borderBottomWidth: 1,
},
title: {
fontSize: 30,
alignSelf: 'center',
marginBottom: 30
},
buttonText: {
fontSize: 18,
color: 'white',
alignSelf: 'center'
},
button: {
height: 36,
backgroundColor: '#48BBEC',
borderColor: '#48BBEC',
borderWidth: 1,
borderRadius: 8,
marginBottom: 10,
alignSelf: 'stretch',
justifyContent: 'center'
},
login: {
flex: 1,
backgroundColor: '#efeff2',
alignItems: 'center',
justifyContent: 'center',
},
image: {
position: 'absolute',
top: 0,
bottom: 0,
left: 30,`enter code here`
right: 30,
},
});
AppRegistry.registerComponent('FirstRun', () => AwesomeProject);
This line... 这条线...
(response) => response.json()
assumes that you are getting a string that is parseable into JSON. 假设您正在获取可解析为JSON的字符串。
Try inspecting the response
. 尝试检查
response
。
Pleas find the code snippet for the simple fetch api call in react.make sure you have valid url which will return you the response 请在React中找到简单的提取api调用的代码段。确保您具有有效的URL,它将返回响应
getMoviesFromApiAsync = () => {
return fetch('http://droidtute.com/reactexample/sample_api/getMovieList.php')
.then((response) => response.json())
.then((responseJson) => {
// alert(JSON.stringify(responseJson))
this.setState({ moviesList: responseJson.movieList }) // this will update state to re-render ui
return responseJson.movieList;
})
.catch((error) => {
console.error(error);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.