2

I'm using Great Schools Api :

fetch("https://api.greatschools.org/search/schools?key=***********&state=CA&q=cupertino&levelCode=elementary-schools&limit=1",{ 
    method: 'GET',
    Accept:'application/xml',
    headers : new Headers ({
      'content-type': 'application/x-www-form-urlencoded',
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, PUT',
      'Access-Control-Allow-Headers': 'Content-Type',  
    }),
    mode:'no-cors'
    })
    .then(response => {
      console.log(response, 'data')
    })

console for response is :

Response
bodyUsed: false
headers: Headers {  }
ok: false
redirected: false
status: 0
statusText: ""
type: "opaque"
url: ""

But in Browser Network console I'm geting correct XMl response.

How to get correct response.

2 Answers 2

1

You are logging the response object.

You need to access the response body.

You can do this by returning response.text()

From the fetch docs

Body.text() Takes a Response stream and reads it to completion. It returns a promise that resolves with a USVString (text).

fetch("https://api.greatschools.org/search/schools?key=***********&state=CA&q=cupertino&levelCode=elementary-schools&limit=1",{ 
    method: 'GET',
    headers : new Headers ({
      'Accept:'application/xml',
      'content-type': 'application/x-www-form-urlencoded',
      'Access-Control-Allow-Origin': '*',
      'Access-Control-Allow-Methods': 'GET, POST, PUT',
      'Access-Control-Allow-Headers': 'Content-Type',  
    }),
    mode:'no-cors'
    })
    .then(response => {
      return response.text()
    })
    .then(xml => {
      console.log("xml", xml)
    })
Sign up to request clarification or add additional context in comments.

9 Comments

Still no use. Just string 'xml' is getting printed in the console.
Can you make an API request in postman and show us the result?
API : api.greatschools.org/search/schools?key=************&state=CA&q=cupertino&levelCode=elementary-schools&limit=3 , Response: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <schools> <school> <gsId>9094</gsId> <name>St. Joseph of Cupertino Elementary School</name> <type>private</type> <lat>37.3243</lat> <lon>-122.032</lon> </school> </schools>
Have updated my post. You need to move the accept property to the headers object.
No Result, only "xml" is getting printed in the console
|
1

When the Response's type is opaque as shared by you, it means that the request is still blocked by cors. The browser's network tab shows you the request's response but the fetch's callback would only show you opaque response. Additionally, response.ok would be true if the fetch is actually successful and the response is ready to be consumed in the callback

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.