简体   繁体   English

Javascript:如何通过HTTPS和AJAX调用API?

[英]Javascript: How to call an API over HTTPS and AJAX?

Previously my website was on http protocol, so doing this in my javascript worked: 以前我的网站使用的是http协议,因此在我的JavaScript中可以正常运行:

$.ajax({
                    url: 'http://x.x.x:5000/sthng',
                    type: 'GET',
                    headers: {
                        "Authorization": "Basic " + btoa(uname + ":" + pass)
                    },
                    success: function(result) {
                        console.log(result)
                    }
                 });

But as http is not secure, I shifted to HTTPS. 但是由于http不安全,所以我转向了HTTPS。 How should I make the ajax call when I'm trying to access HTTPS website? 尝试访问HTTPS网站时应如何进行ajax调用?

Btw I'm using Tornado web server which is in front of Flask API. 顺便说一句,我正在使用Flask API前面的Tornado Web服务器。 My Tornado server code is like this: 我的Tornado服务器代码是这样的:

from tornado.wsgi import WSGIContainer
from tornado.ioloop import IOLoop
from tornado.web import FallbackHandler, RequestHandler, Application
from flask-file import app
import tornado.httpserver

class MainHandler(RequestHandler):
  def get(self):
    self.write("This message comes from Tornado ^_^")

class getToken(tornado.web.RequestHandler):
    def get(self):
        self.write("hello")

tr = WSGIContainer(app)

application = Application([
(r'/', getToken),
(r"/tornado", MainHandler),
(r".*", FallbackHandler, dict(fallback=tr)),
])

if __name__ == "__main__":
  http_server = tornado.httpserver.HTTPServer(application, ssl_options={
        "certfile": "C:/x/key.pem",
        "keyfile": "C:/x/server.key",
  })
  http_server.listen(5000)
  tornado.ioloop.IOLoop.instance().start()

This is the error I'm getting in Tornado every time Javascript (using url: ' https://xxx:5000/sthng ' in AJAX) calls the API: 这是我每次Javascript(在AJAX中使用url:' https:// xxx:5000 / sthng ')调用API时都会在Tornado中遇到的错误:

ERROR:tornado.application:Exception in callback (<socket.socket fd=344, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('0.0.0.0', 5000)>, <function wrap.<locals>.null_wrapper at 0x000000000499FD90>) Traceback (most recent call last):   
File "C:\Python34\lib\site-packages\tornado\ioloop.py", line 883, in
    start handler_func(fd_obj, events)  
File "C:\Python34\lib\site-packages\tornado\stack_context.py", line 275, in
    null_wrapper  return fn(*args, **kwargs)
File "C:\Python34\lib\site-packages\tornado\netutil.py", line 274, in
    accept_handler callback(connection, address)
File "C:\Python34\lib\site-packages\tornado\tcpserver.py", line 239, in
    _handle_connection do_handshake_on_connect=False) 
File "C:\Python34\lib\site-packages\tornado\netutil.py", line 510, in
    ssl_wrap_socket context = ssl_options_to_context(ssl_options)
File "C:\Python34\lib\site-packages\tornado\netutil.py", line 487, in
    ssl_options_to_context context.load_cert_chain(ssl_options['certfile'],
    ssl_options.get('keyfile', None)) ssl.SSLError: [SSL] PEM lib (_ssl.c:2515)

Also: doing curl gives me this: curl: (35) Unknown SSL protocol error in connection to xxx:5000 另外:执行curl给我这样的事情:curl:(35)连接到xxx:5000的未知SSL协议错误

Really lost here! 真的在这里迷路了! Please help. 请帮忙。 Thanks. 谢谢。

Try setting the datatype to "jsonp", that's helped me for cross-origin requests. 尝试将数据类型设置为“ jsonp”,这对跨域请求很有帮助。

  $.ajax({
        url: "https://x.x.x:5000/sthng/"
        dataType: "jsonp",
        success: function(data) {
          console.log(data)
        }
 });

no need of type: get as ajax call defaults to 'get' 不需要类型:ajax调用的get默认为'get'

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

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