jQuery ajax加上chrome中的python flask给出了错误net :: ERR_INSECURE_RESPONSE

时间:2018-01-06 19:56:54

标签: javascript jquery python ajax google-chrome-extension

我正在尝试编写我的第一个Chrome扩展程序。不幸的是,我的javascript知识和技能非常有限。因此,我希望通过完整的工作代码简单回答。

如何从Chrome扩展程序向localhost:1234发送JSON?实际上我只想发送字符串,但如果它将以JSON打包,那么在服务器端解析会更容易。实际上我的服务器只是将这些字符串附加到txt文件(尚未实现),但似乎几乎不可能从chrome扩展访问文件系统。因此,这已经是一个解决方法,因为我认为通过Web浏览器和它的扩展来联系服务器是很简单的。

我虽然已经从这里找到了一个完美的解决方案:How can i send data from chrome extension to localhost?我认为:完美,Websocket是html5标准的一部分,为什么我之前没有找到这个解决方案。不幸的是,chrome给了我这个错误:

  

尝试安装此扩展程序时出现警告:'套接字'   仅适用于打包的应用,但这是一个扩展。

现在我切换到第二个选项Ajax,并且进一步发展。 这是script.js:

$(document).ready(function() {
  $("a:contains('BibTeX')").click(function(e) {
    var $a = $(this);
    $.get(this.href, function(data) {
      var $b = $(this);

      $.ajax({
        type: 'POST',
        url: 'https://localhost:5000/api/append_bib_file',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({bibentry: data}),
      });

      $a.html("Sended to server");
      setTimeout(function() {$a.html("Append to BibTeX file")}, 1000);
    });
    e.preventDefault();
    });
  }).html("Append to BibTeX file");

这是append_file_server.py

from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')


@app.route('/api/append_bib_file', methods=['POST'])
def say_name():
    json = request.get_json()
    bibitem = json['bibitem']
    print("Hello World!!!")
    print(bibitem)
    return jsonify(hello="Hello World!!!")


if __name__ == '__main__':
    app.run(debug=True,ssl_context=('/Users/tfrondel/.localhost-ssl/cert.pem','/Users/tfrondel/.localhost-ssl/key.pem'))

这是manifest.json

{
  "name": "Google Scholar Write Bibtex",
  "version": "0.1.0",
  "description": "Show Bibtex on Google Scholar Search Page",
  "content_scripts": [
    {
      "matches": [
        "https://scholar.google.be/*", "http://scholar.google.be/*",
        "https://scholar.google.ca/*", "http://scholar.google.ca/*",
        "https://scholar.google.com.hk/*", "http://scholar.google.com.hk/*",
        "https://scholar.google.com/*", "http://scholar.google.com/*",
        "https://scholar.google.co.jp/*", "http://scholar.google.co.jp/*",
        "https://scholar.google.co.uk/*", "http://scholar.google.co.uk/*",
        "https://scholar.google.de/*", "http://scholar.google.de/*",
        "https://scholar.google.dk/*", "http://scholar.google.dk/*",
        "https://scholar.google.es/*", "http://scholar.google.es/*",
        "https://scholar.google.fi/*", "http://scholar.google.fi/*",
        "https://scholar.google.fr/*", "http://scholar.google.fr/*",
        "https://scholar.google.it/*", "http://scholar.google.it/*",
        "https://scholar.google.nl/*", "http://scholar.google.nl/*",
        "https://scholar.google.no/*", "http://scholar.google.no/*",
        "https://scholar.google.pt/*", "http://scholar.google.pt/*",
        "https://scholar.google.se/*", "http://scholar.google.se/*"
      ],
      "js": ["jquery-3.2.1.min.js", "clipboard.min.js", "script.js"]
    }
  ],
  "permissions":  [
    "tabs",
    "http://*/*",
    "https://*/*",
    "clipboardWrite" ,
    "http://localhost/*"
    "https://localhost/*"
  ],

  "manifest_version": 2
}

这是错误消息:

  

jquery-3.2.1.min.js:4 POST https://localhost:5000/api/append_bib_file   净:: ERR_INSECURE_RESPONSE

我希望我有一个简单的初学者的错误,可以轻松修复。

0 个答案:

没有答案