如何使用Javascript / jQuery的(阿贾克斯)检索结果谷歌QPX? [英] How to retrieve result for Google QPX with Javascript/jQuery (Ajax)?

查看:148
本文介绍了如何使用Javascript / jQuery的(阿贾克斯)检索结果谷歌QPX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注册了谷歌API控制台并设置帐号和API密钥,但我的问题是如何重新获取来自谷歌QPX的结果。 是什么原因导致下面的错误?

设置的JSON的查询谷歌请求

  VAR FlightRequest = {
  请求: {
    片: [
      {
        原点:DCA,
        目标:LAX
        日期:2015年2月11号
      }
    ]
    乘客:{
      adultCount:1,
      infantInLapCount:0,
      infantInSeatCount:0,
      childCount:0,
      seniorCount:0
    },
    解决方案:20,
    退还:假的
  }
}
 

请求数据,并返回它。

  $。阿贾克斯({
 网址:https://www.googleapis.com/qpxEx$p$pss/v1/trips/search?key=XXXXXXXXXXXXXXXX
 键入:POST,
 数据类型:JSON,
 的contentType:应用/ JSON的,
 数据:FlightRequest,
 成功:功能(数据){
  警报(JSON.stringify(数据));
},
  错误:函数(){
   警报(不能获取数据);
 }
});
 

错误: 我已经检查了我的API密钥和是正确的。什么会导致这个问题?

  

400状态(错误的请求)

解决方案

我找出问题通过使用谷歌浏览器<一href="https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en"相对=nofollow>邮差应用,并使用 JSON.stringify(); 转换成谷歌JSON发送请求(对象)到字符串 $阿贾克斯();这里是一个步骤,以解决这个问题与jQuery

首先创建一个变量为您的谷歌JSON请求: 我们将利用这个使用Ajax来检索数据。

  VAR FlightRequest = {
      请求: {
        片: [
          {
            原点:DCA,
            目标:LAX
            日期:2015年2月11号
          }
        ]
        乘客:{
          adultCount:1,
          infantInLapCount:0,
          infantInSeatCount:0,
          childCount:0,
          seniorCount:0
        },
        解决方案:20,
        退还:假的
      }
    };
 

使用jQuery的 $阿贾克斯(); 发送使用键 内容类型数据请求

  $。阿贾克斯({
     键入:POST,
     //设置您请求的URL和API密钥。
     网址:https://www.googleapis.com/qpxEx$p$pss/v1/trips/search?key=YOUR-API-KEY
     的contentType:应用/ JSON',//设置内容类型:应用程序/ JSON
     数据类型:JSON,
     //我们从谷歌QPX想查询,这将是我们在创建之初的变量
     数据:JSON.stringify(FlightRequest)
     成功:功能(数据){
      //一旦我们得到的结果,你可以将其发送至控制台或你喜欢的任何地方使用它。
      的console.log(JSON.stringify(数据));
    },
      错误:函数(){
       //错误处理对我们的要求
       警报(访问谷歌QPX失败。);
     }
    });
 

I have already signed up for Google API Console and set up the account and API key, but my question is how do I retrieve the results from Google QPX. What causes the error below?

Setting up the json Query for Google Request

var FlightRequest = {
  "request": {
    "slice": [
      {
        "origin": "DCA",
        "destination": "LAX",
        "date": "2015-02-11"
      }
    ],
    "passengers": {
      "adultCount": 1,
      "infantInLapCount": 0,
      "infantInSeatCount": 0,
      "childCount": 0,
      "seniorCount": 0
    },
    "solutions": 20,
    "refundable": false
  }
}

Requesting the Data and Returning it.

$.ajax({
 url: "https://www.googleapis.com/qpxExpress/v1/trips/search?key=XXXXXXXXXXXXXXXX", 
 type: 'POST',
 dataType: 'json',
 contentType: 'application/json',
 data: FlightRequest,
 success: function (data) {
  alert(JSON.stringify(data));
},
  error: function(){
   alert("Cannot get data");
 }
});

Error: I have already checked my API Key and is Correct. What can cause this issue?

status of 400 (Bad Request)

解决方案

I have figure out the problem by using Google Chrome POSTMAN App and Using JSON.stringify(); to convert Google json send request (Object) into string for $.ajax(); here is a steps to solve this problem with jQuery.

Start by creating a Variable for your Google json request: We will use this with Ajax to retrieve the data.

var FlightRequest = {
      "request": {
        "slice": [
          {
            "origin": "DCA",
            "destination": "LAX",
            "date": "2015-02-11"
          }
        ],
        "passengers": {
          "adultCount": 1,
          "infantInLapCount": 0,
          "infantInSeatCount": 0,
          "childCount": 0,
          "seniorCount": 0
        },
        "solutions": 20,
        "refundable": false
      }
    };

Use jQuery $.ajax(); to send Access Key, content-type and data requesting

$.ajax({
     type: "POST",
     //Set up your request URL and API Key.
     url: "https://www.googleapis.com/qpxExpress/v1/trips/search?key=YOUR-API-KEY", 
     contentType: 'application/json', // Set Content-type: application/json
     dataType: 'json',
     // The query we want from Google QPX, This will be the variable we created in the beginning
     data: JSON.stringify(FlightRequest),
     success: function (data) {
      //Once we get the result you can either send it to console or use it anywhere you like.
      console.log(JSON.stringify(data));
    },
      error: function(){
       //Error Handling for our request
       alert("Access to Google QPX Failed.");
     }
    });

这篇关于如何使用Javascript / jQuery的(阿贾克斯)检索结果谷歌QPX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆