POST Request works with Postman but fails in fetch?(POST 请求与 Postman 一起使用但获取失败?)
问题描述
我创建了后端,它将接受对以下 url 的请求
I created backend where it will accept requests to the following url
http://10.1.0.1/cgi-bin/api/write_config
使用 Postman,我得到了适当的响应,但是当我尝试使用 fetch 做同样的事情时,我得到一个网络错误
With Postman I get the appropriate response but when I try the same thing with fetch, I get a network error
这是我的代码:
fetch ('http://10.1.0.1/cgi-bin/api/write_config', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
param1: 'attr1',
param2: 'attr2',
param3: 'attr3',
}),
})
.then(response => {
console.log(response.json())
return response.json()
})
.then(data => {
console.log(data)
})
.catch(e => {
console.log('In catch block')
console.log(e)
})
这些是我从响应中获得的标题(在 Postman 中):
These are the header I'm getting from the response (in Postman):
内容类型:应用程序/json访问控制允许来源:*
Content-Type: application/json Access-Control-Allow-Origin: *
我收到以下错误(在 Javascript 中):
I'm getting the following error (in Javascript):
{ [TypeError: 网络请求失败] 行:24117,列:31,
来源网址:'http://localhost:8081/index.delta?platform=android&dev=true&minify=false'}
{ [TypeError: Network request failed] line: 24117, column: 31,
sourceURL: 'http://localhost:8081/index.delta?platform=android&dev=true&minify=false' }
我以与配置其他设备相同的方式配置 Raspberry Pi,然后输出正确.是 Javascript 的一些证书问题吗?
I configured the Raspberry Pi in the same way in which I configured the other device then the output is correct. Is it some certificate issue with Javascript?
推荐答案
你试过 console.log(body) 吗?有时 JSON 不起作用.如果你的身体没有问题.我确定标题是错误的
did u tried console.log(body) ? sometimes JSON not work. if u don't have problem with body . I'm sure header is wrong
这篇关于POST 请求与 Postman 一起使用但获取失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:POST 请求与 Postman 一起使用但获取失败?


基础教程推荐
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01