php - 在 react nativ fetch 方法中不起作用
问题描述
我正在尝试从 php api 获取数据这里是 api 的代码
<?php
if(!empty($_POST)){
$array = ["name" => "vasu" ,"json" => "created at 2021"];
print_r(json_encode($array));
}else{
$array = ["error" => 22];
print_r(json_encode($array));
}
?>
这是我的 react nativ fetch 方法的代码
fetch(url,{
method : "POST",
body : JSON.stringify({
name : "vasu"
})
})
.then((response) => response.json())
.then((json) => {
console.log(json);
})
.catch((error) => {
console.error(error);
});
输出
{“错误”:22}
这意味着我无法在 $_POST 中得到任何东西 如何解决这个问题
解决方案
您好问题是您在声明您的身体不正确的同时使用JSON.stringyfy这就是为什么它没有获取身体的价值并给您错误
你应该用它作为
fetch(url,{
method : "POST",
body : {
name : "vasu"
}
})
.then((response) => response.json())
.then((json) => {
console.log(json);
})
.catch((error) => {
console.error(error);
});
推荐阅读
- algorithm - 在多张发票上分配一笔款项
- django - django-rest ModelSerializer 选择要在嵌套关系中显示的字段
- android - 如何在 kotlin 中使用 http 请求将图像上传到服务器?
- r - 在 xgboost 中使用文档术语矩阵
- firebase - Firebase 用户属性 - 如何正确存储列表值?
- vue.js - 如何解决 Vue 生产构建中的 CORS 策略问题?
- opencart-3 - 如何对我在 opencart 3.x 中创建的模块进行扩展?
- rest - 查询参数 AND 和 OR 区分
- c# - 使用 Office 互操作启动 Outlook 的问题
- mysql - 如何在mysql中选择喜欢通配符