如何将JSON对象从React Native发送到Laravel服务器?
我想将这些JSON对象发送到API,以便将它们保存在数据库中: JSON对象 来自React Native的Post请求: 当我提交函数时,我有一个500错误代码 拉威尔控制器: 当我尝试用失眠发送此邮件时,它正在工作,那么我应该怎么做如何将JSON对象从React Native发送到Laravel服务器?,json,laravel,react-native,post,axios,Json,Laravel,React Native,Post,Axios,我想将这些JSON对象发送到API,以便将它们保存在数据库中: JSON对象 来自React Native的Post请求: 当我提交函数时,我有一个500错误代码 拉威尔控制器: 当我尝试用失眠发送此邮件时,它正在工作,那么我应该怎么做 [ { "name": "Michel", "phone": "+213 93 783 28 73", "users_id": "1"
[
{ "name": "Michel", "phone": "+213 93 783 28 73", "users_id": "1"},
{ "name": "Annie", "phone": "+213 93 783 28 72", "users_id": "1"},
{ "name": "Rory", "phone": "+16505551212", "users_id": "1"},
{ "name": "CESI", "phone": "+213 93 783 28 73", "users_id": "1"}
]
通过不同文件中相同或不同的React Native中的Fetch API创建服务 i、 e.service.js
const api_url = "www.myUrl.com/"
export function submitContact(data) {
return fetch(api_url+'/api/contact', {
method: 'POST',
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json',
},
body: JSON.stringify(data),
});
}
然后导入并在组件内部调用它
import { SubmitContact} from './service'
export default function Contact({ navigation }) {
const { user, logout } = useContext(AuthContext)
const [contact, setContact] = useState([]);
const [count, setCount] = useState('');
//console.log(utility.con)
const uticontact = utility.con;
console.log(uticontact)
//Send Post Request
submitContact(utility.con).then((response) => response.json())
.then((responseJson) => {
// perform some action on your response
}) ;
};
[
{ "name": "Michel", "phone": "+213 93 783 28 73", "users_id": "1"},
{ "name": "Annie", "phone": "+213 93 783 28 72", "users_id": "1"},
{ "name": "Rory", "phone": "+16505551212", "users_id": "1"},
{ "name": "CESI", "phone": "+213 93 783 28 73", "users_id": "1"}
]
const api_url = "www.myUrl.com/"
export function submitContact(data) {
return fetch(api_url+'/api/contact', {
method: 'POST',
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json',
},
body: JSON.stringify(data),
});
}
import { SubmitContact} from './service'
export default function Contact({ navigation }) {
const { user, logout } = useContext(AuthContext)
const [contact, setContact] = useState([]);
const [count, setCount] = useState('');
//console.log(utility.con)
const uticontact = utility.con;
console.log(uticontact)
//Send Post Request
submitContact(utility.con).then((response) => response.json())
.then((responseJson) => {
// perform some action on your response
}) ;
};