当我试图发布来自react原生于PHP的数据时,PHP本机显示错误:
Json分析错误:未识别的令牌“<”
我用头类型为'application/json‘的postman测试了PHP,它工作得很好,这里是react本机代码,有人能帮我吗?提前感谢!
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
ActivityIndicatorIOS,
TextInput,
TouchableOpacity,
} from 'react-native';
const REQUEST_URL = 'http://localhost:8000/user';
export default class extends Component {
constructor(props) {
super(props);
}
_submit() {
fetch(REQUEST_URL, {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstname: "Justin", lastname: "Robot"
})
})
.then((response) => response.json())
.then((responseData) => {
console.log(responseData.body);
})
.done();
}
render() {
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.submitButton}
onPress={() => this._submit()}
>
<Text>http post</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
submitButton: {
backgroundColor: 'lightskyblue',
borderRadius: 5,
paddingTop: 5,
paddingBottom: 5,
paddingLeft: 20,
paddingRight: 20,
}
});发布于 2016-06-17 17:30:09
我们只是在Reacti原住民中遇到了这个问题,因为我们的服务器通过HTML返回了一个错误响应。
<html>
<head><title>413 Request Entity Too Large</title></head>
<body bgcolor="white">
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx</center>
</body>
</html>这些修正可以是以下任何一种:
1)防止服务器端代码中发生错误。
2)在服务器上进行更好的错误处理,以返回JSON错误,而不是HTML错误。
3)编写客户端代码以检测HTML是否被返回,并显示出更有用的错误信息。
https://stackoverflow.com/questions/37797822
复制相似问题