我正在我的 codeigniter 项目中使用 fetch 发出发布请求。请求看起来像这样
fetch('myurl/mycontroller', {
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
testdata: 123,
})
}).then((res) => {
console.log(res);
}).catch(console.log);
我的控制器如下所示
class MyController extends CI_Controller
{
public function mycontroller()
{
$data = $this->input->post('testdata');
return $data . " is the passed data.";
}
}
但数据没有传递到我的控制器。我回声$_POST
它给了我一个空数组。知道我做错了什么吗?我正在使用 codeigniter 2 (我知道它现在已经很旧了)
所以不完全确定实际原因,但 codeigniter 的 CI 核心可能存在一些错误,它不会使用 fetch 解析传递给控制器的数据。使用FormData()
and axios
我能够解决这个问题。
var postData = new FormData();
postData.append('testdata', 123);
axios.post('myurl/mycontroller', postData).then(function(response){
console.log("success:", response);
}).catch(function(error){
console.log("error:", error);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)