如果不允许使用"POST"方法,可以使用其他HTTP方法来替代。以下是一些常用的HTTP方法及其用途:
fetch('/api/resource?param1=value1¶m2=value2', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
fetch('/api/resource/123', {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ property1: 'value1', property2: 'value2' })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
fetch('/api/resource/123', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
注意:使用这些方法时,需要确保服务器端的API能够正确处理相应的请求方法。