简体   繁体   中英

POST Request works with Postman but fails in fetch?

I created backend where it will accept requests to the following url

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

With Postman I get the appropriate response but when I try the same thing with fetch, I get a network error

This is my code:


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)
      })

These are the header I'm getting from the response (in Postman):

Content-Type: application/json Access-Control-Allow-Origin: *

I'm getting the following error (in Javascript):

{ [TypeError: Network request failed] line: 24117, column: 31,
sourceURL: ' http://localhost:8081/index.delta?platform=android&dev=true&minify=false ' }

Edit:

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?

If you are using the Android Emulator try to use your IP Address instead of 10.1.0.1 - I had network request errors too but it worked with my IP.

did u tried console.log(body)? sometimes JSON not work. if u don't have problem with body. I'm sure header is wrong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM