How to get data from window.fetch () request?

I am trying to use window.fetch () to get json from the server, but cannot get the data from the response. I have this code:

> let url =
> 'https://api.flightstats.com/flex/schedules/rest/v1/json/from/SYD/to/MEL/departing/2016/3/28?appId=f4b1b6c9&appKey=59bd8f0274f2ae88aebd2c1db7794f7f';
> let request = new Request (url, {
>         method: 'GET',
>         mode: 'no-cors'
>     });
> 
>  fetch(request)
>         .then(function(response){
>             console.log(response)
>         });

It seems that this request is successful, I see the status of 200 and the response body with json on the tab in the network - status and response . But in console.logi don't see json object - console log image

I can not understand why I do not see json in console.log

+4
source share
2 answers

, , CORS. , fetch(), . fetch() mode: 'cors', , CORS, .

mode: 'no-cors', ( ), , opaque.

api.flightstats.com , JSONP, CORS .

, , jQuery, , JSONP . . . URL- /json/ /jsonp/ dataType: "jsonp" jQuery. jQuery callback=xxxxx script ( JSONP).

var url =
'https://api.flightstats.com/flex/schedules/rest/v1/jsonp/from/SYD/to/MEL/departing/2016/3/28?appId=f4b1b6c9&appKey=59bd8f0274f2ae88aebd2c1db7794f7f';

$.ajax(url, {dataType: "jsonp"}).then(function(response) {
	log(response);
}, function(err) {
    log("$.ajax() error:")
	log(err);
})
<script src="http://files.the-friend-family.com/log.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Hide result
+5

Fetch API; , API :

  • ArrayBuffer()
  • ()
  • JSON()
  • ()
  • FormData()

, JSON (, , , ), response.json() . Promise, Fetch API.

response.json().then(function(data) {  
    console.log(data);  
});
+4

All Articles