ajax 在 javascript 中请求 Microsoft Emotion API

ajax request to Microsoft Emotion API in javascript

我正在尝试 microsoft emotion api。我是 运行 一个启用了 CORS 的简单 python Web 服务器。下面是我用来启动服务器的服务器 python 文件:

python-server.py

#! /usr/bin/env python2
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer

class CORSRequestHandler (SimpleHTTPRequestHandler):
    def end_headers (self):
        self.send_header('Access-Control-Allow-Origin', '*')
        SimpleHTTPRequestHandler.end_headers(self)

if __name__ == '__main__':
    BaseHTTPServer.test(CORSRequestHandler, BaseHTTPServer.HTTPServer)

我有一个 index.html 文件,我在其中发送 http 请求:

index.html

<!DOCTYPE html>
<html>
<head>
    <title>JSSample</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>

<script type="text/javascript">
    $(function() {
        $.ajax({
            url: "https://api.projectoxford.ai/emotion/v1.0/recognize",
            beforeSend: function(xhrObj){
                // Request headers
                xhrObj.setRequestHeader("Content-Type","application/json");
                xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key",JSON.stringify({"my-key"}));
            },
            type: "POST",
            // Request body
            data: JSON.stringify({"url": "http://tinyurl.com/2g9mqh"}),
        })
        .done(function(data) {
            alert("success");
        })
        .fail(function() {
            alert("error");
        });
    });
</script>
</body>
</html>

大约 30 秒后,我收到连接被拒绝的响应。 http请求代码取自我之前链接的情感api的页面。我想知道我是否需要一个真正的服务器或者代码有错误吗?谢谢。

请使用下面的代码(替换您的密钥),只需将其另存为 .html 并在浏览器中打开它即可关闭工作(无需任何服务器)。如果有效,请在您的 python 服务器中尝试。

<!DOCTYPE html>
<html>
<head>
    <title>JSSample</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
    $(function() {
        $.ajax({
            url: "https://api.projectoxford.ai/emotion/v1.0/recognize",
            beforeSend: function(xhrObj){
                // Request headers
                xhrObj.setRequestHeader("Content-Type","application/json");
                xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","your-key");
            },
            type: "POST",
            // Request body
            data: {"url": "https://oxfordportal.blob.core.windows.net/emotion/recognition1.jpg"},
        })
        .done(function(data) {
            alert("success");
        })
        .fail(function() {
            alert("error");
        });
    });
</script>
</body>
</html>

JSON需要以字符串形式发送出去。因此,将您的 body 规格更改为:

data: "{\"url\": \"http://...\"}"

<!DOCTYPE html>
<html>

<head>
  <title>Face API</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
</head>

<body>

  <script type="text/javascript">
    $(function() {
      $.ajax({
          url: "https://api.projectoxford.ai/emotion/v1.0/recognize",
          beforeSend: function(xhrObj) {
            // Request headers
            xhrObj.setRequestHeader("Content-Type", "application/json");
            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key",
              "");
          },
          type: "POST",
          // Request body
          data: JSON.stringify({
            "url": "http://i1.mirror.co.uk/incoming/article6395000.ece/ALTERNATES/s1200/MAIN-David-Beckham-next-James-Bond.jpg"
          }),
        })
        .done(function(data) {
          console.log(data);
        })
        .fail(function(e) {
          console.log(e);
        });
    });
  </script>
</body>

</html>