POST请求可与Postman一起使用,但无法提取?

时间:2019-11-20 10:37:32

标签: javascript react-native networking postman react-native-android

我创建了后端,它将在其中接受对以下网址的请求

  

http://10.1.0.1/cgi-bin/api/write_config

使用Postman可以得到适当的响应,但是当我尝试使用fetch进行相同操作时,会出现网络错误

这是我的代码:


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中):

内容类型:application / json 访问控制允许来源:*

我收到以下错误(使用Javascript):

  

{[TypeError:网络请求失败]行:24117,列:31,
  sourceURL:   'http://localhost:8081/index.delta?platform=android&dev=true&minify=false'   }

编辑:

我以与配置其他设备相同的方式配置Raspberry Pi,然后输出正确。是Javascript的证书问题吗?

2 个答案:

答案 0 :(得分:0)

如果您使用的是Android模拟器,请尝试使用您的IP地址而不是10.1.0.1-我也遇到了网络请求错误,但它可以与我的IP一起使用。

答案 1 :(得分:0)

您是否尝试过console.log(body)?有时JSON无法正常工作。如果你身体没有问题。我确定标题错误

相关问题