javascript - 在 javascript 中使用 API 而不使用 Node.js

标签 javascript jquery node.js cordova windows-phone-8

我正在我的 Windows Phone 8 应用程序中利用 CamFind 的 API 进行图像识别。在他们的网站上,他们给出了如何将 API 与 Node.js 一起使用的示例。但是我正在编写 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 请求示例(上面的第一个代码块)方面是否做错了什么?我如何尝试通过普通的 Javascript 使用 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")
          }
      });
  }

关于javascript - 在 javascript 中使用 API 而不使用 Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20809693/

相关文章:

javascript - 如何在 firefox 中按下 shift 时禁用鼠标右键单击菜单?

javascript - 有没有办法添加在选择菜单中浏览文件的功能?

javascript - jquery 使用 $(this) 不起作用

javascript - 淡入/淡出、向上/向下滑动方法重复/完成得太快

javascript - 循环遍历对象直到找到参数(VueJS)

javascript - contenteditable div 中突出显示的文本 : Get the start and the end of the highlighted text

node.js - 如何将 firestore 时间戳转换为人类可读的日期格式

node.js - 如何在 Node.js 中使用 http.request 限制响应长度

javascript - 如何在 node.js 中创建对象数组

当使用溢出 :hidden to outer div 时,jquery 选择的选择插件不能很好地显示下拉菜单