繁体   English   中英

在不使用Node.js的情况下在JavaScript中使用API

[英]Consuming API in javascript without using Node.js

我在Windows Phone 8应用中利用CamFind的API进行图像识别。 在他们的网站上,他们给出了如何与Node.js一起使用API​​的示例。但是,我正在编写PhoneGap Windows Phone应用程序,但没有此功能。

我只想使用简单的jquery / javascript来使用此API。

这是他们网站上提供的示例:

var Request = unirest.post("https://camfind.p.mashape.com/image_requests")
  .headers({ 
    "X-Mashape-Authorization": "Z**********************"
  })
  .send({ 
    "image_request[locale]": "en_US",
    "image_request[language]": "en",
    "image_request[device_id]": "<image_request[device_id]>",
    "image_request[latitude]": "35.8714220766008",
    "image_request[longitude]": "14.3583203002251",
    "image_request[altitude]": "27.912109375",
    "focus[x]": "480",
    "focus[y]": "640",
    "image_request[image]": "/tmp/file.path"
  })
  .end(function (response) {
    console.log(response);
  });

这是我尝试使用jquery /'plain'javascript做同样的事情

$.ajax({
    url: 'https://camfind.p.mashape.com/image_requests', // The URL to the API. You can get this by clicking on "Show CURL example" from an API profile
    type: 'POST', // The HTTP Method
    data: {
    "image_request[locale]": "en_US",
    "image_request[language]": "en",
    "image_request[device_id]": "<image_request[device_id]>",
    "image_request[latitude]": "35.8714220766008",
    "image_request[longitude]": "14.3583203002251",
    "image_request[altitude]": "27.912109375",
    "focus[x]": "480",
    "focus[y]": "640",
    "image_request[image]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"

}, // Additional parameters here
    datatype: 'json',
    success: function(data) { alert(JSON.stringify(data)); },
    error: function(err) { alert(err); },
    beforeSend: function(xhr) {
    xhr.setRequestHeader("X-Mashape-Authorization", "Z**********************");
    }
});

问题/问题:

  • 当我通过javascript / jquery进行操作时-似乎在抱怨缺少image_request [image]属性。 它永远不会成功。
  • 就我如何转换CamFind VS提供的Node.js API请求示例(上面的第一段代码)而言,我做错了什么。 我如何做通过Java(上面的第二块代码)通过普通方式消费API?

谢谢!!

仅供参考,我正在使用的参考:

我知道这是一个古老的问题,但是在自己尝试解决此问题时偶然发现了这个问题,我认为我应该为将来回答。

问题是此行:

"image_request[image]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"

它应该是:

"image_request[remote_image_url]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"

因此完整的代码是:

$.ajax({
      url: 'https://camfind.p.mashape.com/image_requests', // The URL to the API. You can get this by clicking on "Show CURL example" from an API profile
      type: 'POST', // The HTTP Method
      data: {
      "image_request[locale]": "en_US",
      "image_request[language]": "en",
      "image_request[device_id]": "<image_request[device_id]>",
      "image_request[latitude]": "35.8714220766008",
      "image_request[longitude]": "14.3583203002251",
      "image_request[altitude]": "27.912109375",
      "focus[x]": "480",
      "focus[y]": "640",
      "image_request[remote_image_url]": "http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg"

      }, // Additional parameters here
          datatype: 'json',
          success: function(data) { nowDoSomethingFun(data); },
          error: function(err) { alert(err); },
          beforeSend: function(xhr) {
          xhr.setRequestHeader("X-Mashape-Key", "YOURKEY")
          }
      });
  }

我不熟悉此API,但是您可以尝试格式化数据参数,如下所示:

data: {
    image_request: {
        locale: 'en_US',
        language: 'en',
        device_id: '<image_request[device_id]>',
        latitude: '35.8714220766008',
        longitude: '14.3583203002251',
        altitude: '27.912109375',
        image: 'http://exelens.com/blog/wp-content/uploads/2013/03/bmw-car-2013.jpg'
    },
    focus: {
         x: '480',
         y: '640'
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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