我正在我的 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);
Run Code Online (Sandbox Code Playgroud)
我的控制器如下所示
class MyController extends CI_Controller
{
public function mycontroller()
{
$data = $this->input->post('testdata');
return $data . " is the passed data.";
}
}
Run Code Online (Sandbox Code Playgroud)
但数据没有传递到我的控制器。我回显了它$_POST,它给了我一个空数组。知道我做错了什么吗?我正在使用 codeigniter 2 (我知道它现在已经很旧了)
我是新手,并试图自己学习。我开始使用 react-select 在表单上创建下拉列表,现在我试图传递所选选项的值。我的状态是这样的。
this.state = {
part_id: "",
failure: ""
};
Run Code Online (Sandbox Code Playgroud)
然后在我的渲染中
const {
part_id,
failure
} = this.state;
Run Code Online (Sandbox Code Playgroud)
我的表单看起来有 2 个字段
<FormGroup>
<Label for="failure">Failure</Label>
<Input
type="text"
name="failure"
placeholder="Failure"
value={failure}
onChange={this.changeHandler}
required
/>
</FormGroup>
<FormGroup>
<Label for="part_id">Part</Label>
<Select
name="part_id"
value={part_id}
onChange={this.changeHandler}
options={option}
/>
</FormGroup>
Run Code Online (Sandbox Code Playgroud)
在changeHandler这个样子的
changeHandler = e => {
this.setState({ [e.target.name]: e.target.value });
};
Run Code Online (Sandbox Code Playgroud)
更改处理程序对输入工作正常,但 Select 抛出错误,说无法读取属性名称。我浏览了 API 文档,并为 Select onChange 想出了这样的东西
onChange={part_id => this.setState({ part_id })}
Run Code Online (Sandbox Code Playgroud)
它将 part_id 设置为标签、值对。有没有办法只获得价值?以及我将如何实现相同的multiselect?