简体   繁体   中英

API does not collect required data in JavaScript

I'm trying to get route prices with Skyscanner API in JavaScript. I paste the snippet from Rapid API and put it in a function:

function takeData() {
    console.log("to jest test")
    fetch("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2020-08-12?inboundpartialdate=2020-08-23", {
        "method": "GET",
        "headers": {
            "x-rapidapi-host": "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com",
            "x-rapidapi-key": "4ffdf62c6bmshfb49ff445025abep1e2116jsn7d7aae645a00"
        }
    })
    .then(response => {
        console.log(response);
    })
    .catch(err => {
        console.log(err);
    });
}

takeData()

Instead of getting details as shown in Rapid API

在此处输入图像描述

I get this:

Response {type: "cors", url: "https://skyscanner-skyscanner-flight-search-v1.p.r…/JFK-sky/2020-08-12?inboundpartialdate=2020-08-23", redirected: false, status: 200, ok: true, …}
body: (...)
bodyUsed: false
headers: Headers {}
ok: true
redirected: false
status: 200
statusText: "OK"
type: "cors"
url: "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2020-08-12?inboundpartialdate=2020-08-23"
__proto__: Response

Any ideas what may be more to do here? Am I missing something?

you need to parse the response body as JSON:

try this

function takeData() {
    console.log("to jest test")
    fetch("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2020-08-12?inboundpartialdate=2020-08-23", {
        "method": "GET",
        "headers": {
            "x-rapidapi-host": "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com",
            "x-rapidapi-key": "4ffdf62c6bmshfb49ff445025abep1e2116jsn7d7aae645a00"
        }
    })
    .then(response => response.json())
    .then(res => console.log(res))
    .catch(err => {
        console.log(err);
    });
}

takeData()

You are close. You just need to update your fetch call to parse the response so that it returns json . See the second then block.

return response.json();

Full code below:

 fetch("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2020-08-12?inboundpartialdate=2020-08-23", { "method": "GET", "headers": { "x-rapidapi-host": "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com", "x-rapidapi-key": "4ffdf62c6bmshfb49ff445025abep1e2116jsn7d7aae645a00" } }).then(response => { return response.json(); }).then(json => { console.log(json); }).catch(err => { console.log(err); });

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