所以我必须更新一些用户信息,它在邮递员上可以正常工作,但是当我尝试在react-native中键入它时,我必须在fetch方法的主体中做错了什么。在邮递员中,我设置了x-www-form-urlencoded并键入如下键:
键-----值
moto -----测试
这似乎可行,但是当我尝试对我的代码执行相同操作时,我莫名其妙地失败了,这是我的代码
updateUser(){
return fetch(url,{
method: "PATCH",
headers: {
"X-Auth-Token": bearerToken,
"Content-Type":"application/x-www-form-urlencoded"
},
body: JSON.stringify({
moto: this.state.moto
}
})
}
)
我得到200响应,这意味着该调用有效,但是我必须以某种方式将参数moto设置为错误。 有什么想法吗?
答案 0 :(得分:1)
“内容类型”:“应用程序/ x-www-form-urlencoded”
应该是
“ Content-Type”:“ application / json”
答案 1 :(得分:0)
form-urlencoded
与您的body: JSON.stringify()
有所不同。
您将改为使用FormData
对象:
const body = new FormData();
body.append('moto', this.state.moto);
fetch(url, {
method: "PATCH",
headers: {
"X-Auth-Token": bearerToken,
"Content-Type": "application/x-www-form-urlencoded"
},
body,
})
答案 2 :(得分:0)
<select name="item[]" id="item" class="item1" required data-bind="select2data: Helper,class: item" data-placeholder="Item" data-width="100%">
<option></option>
<optgroup label="Uncategorized"></optgroup>
<?php if(!empty($tools)) {
foreach ($tools as $tool) {?>
<option class="itm" value="<?= $tool->id;?>"><?= $tool->tool_name;?></option>
<?php } } ?>
</select>
<td style="vertical-align: top" colspan="2" class="quantity"><input type="text" required name="quantity[]" id="quantity" class="regular form-control input-sm quantity1 " onkeyup="value_check(<?php echo $n++;?>)" style="width: 100px; text-align: right; margin-bottom: 0px; line-height: 14px" data-bind="textInput: Debit" /></td>
<td style="vertical-align: top" class="total" ><input type="text" name="total[]" readonly="readonly" id="total" required class="regular form-control input-sm" style="width: 100px; text-align: right; margin-bottom: 0px; line-height: 14px" /></td>
答案 3 :(得分:0)
通过将主体设置为
最终将其固定 body:
`moto=${this.state.moto}`
似乎urlencoded标头要求使用以下形式的参数: parameter1 = value1&parameter2 = value2
答案 4 :(得分:0)
componentDidMount() {
return fetch(“Your URL”, {
method: 'post',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"Authorization": “token”
},
body: "firstName=Nikhil&favColor=blue&password=easytoguess"
})
.then((response) => response.json())
.then(function (data) {
alert(“Success”)
console.log('Request succeeded with JSON response', data);
})
.catch(function (error) {
alert("error occur")
console.log('Request failed', error);
});
}
答案 5 :(得分:0)
const formData = new FormData();
formData.append('email', 'test@gmail.com');
formData.append('password', '123456');
fetch("https://test.com/api/login", {
method: 'post',
body: formData
})
.then(res => res.json())
.then(
(result) => {
console.log(result);
}).catch(err => {
console.log(err);
})