如何使用';邮政';javascript中的方法
网址---http://eign-backend.herokuapp.com/property/get-property/17/ 我必须写完整的url,比如直到“/17/”或者什么 试试这个如何使用';邮政';javascript中的方法,javascript,fetch,Javascript,Fetch,网址---http://eign-backend.herokuapp.com/property/get-property/17/ 我必须写完整的url,比如直到“/17/”或者什么 试试这个 async function postData(url = '', data = {}) { const response = await fetch(url, { method: 'POST', // *GET, POST, PUT, DELETE, etc. mode: 'c
async function postData(url = '', data = {}) {
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return response.json(); // parses JSON response into native JavaScript objects
}
postData('https://example.com/answer', { answer: 42 })
.then(data => {
console.log(data); // JSON data parsed by `data.json()` call
});
fetch中的第一个参数是实际的url
考虑先阅读一些内容,你可以直接在该URL上发出POST请求。发送一个没有正文的POST请求并使用查询字符串参数是可以的,但是如果不需要正文,那么它应该是get请求而不是POST。但是,如果您的参数包含无效的HTTP字符,则必须对其进行编码
var requestOptions = {
method: 'POST',
redirect: 'follow'
};
fetch("http://eign-backend.herokuapp.com/property/get-property/17/", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
这是我使用的获取调用。您将URL写在显示URL的位置。我们不知道应该在主体中添加什么,因为我们对API一无所知。这取决于
http://eign-backend.herokuapp.com/property/get-property/17/
最有可能起作用。i、 e.等待提取('http://eign-backend.herokuapp.com/property/get-property/17/“,{
为什么?那是错误的URL,(大概)错误的正文,还有一大堆毫无意义的样板文件(我以前在什么地方见过,所以这有抄袭的味道)。
const response=await fetch('http://eign-backend.herokuapp.com/property/get-property/17/',{
method:'POST',
body: JSON.stringify(
{
//what should I write here => write whatever you want to send in this post request's body
}
),
headers:{
headers: {'Content-Type':'application/json'},
}
})
const data=await response.json();
console.log(data);
var requestOptions = {
method: 'POST',
redirect: 'follow'
};
fetch("http://eign-backend.herokuapp.com/property/get-property/17/", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));