我想通过代码转换来获取一个文件。通常这是通过XMLHttpRequest完成的。在Javascript脚本中,新的构造函数用于此目的。我如何继续为它进行代码转换?下面是我不能工作的代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def read_file():
xmlhttp=XMLHttpRequest()
xmlhttp.open('GET', "https://raw.githubusercontent.com/bunkahle/Transcrypt-Examples/master/README.md", False);
xmlhttp.send()
console.log(xmlhttp.responseText)和html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="__javascript__/read_file.js"></script>
<title>Read File</title>
</head>
<body>
<main>
<h1>Read a file!</h1>
<p id="p1" class="para1">Read a file!</p>
<button id="button1" onclick="read_file.read_file()">Click for retrieving text file</button><br><br>
<p id="demo"></p>
</main>
</body>
</html>发布于 2018-02-18 16:11:40
您可以使用__new__函数创建本机JavaScript对象:
(请注意,对于实例化Python/代码转换类,这不是必需的)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def read_file():
xmlhttp= __new__ (XMLHttpRequest())
xmlhttp.open('GET', "https://raw.githubusercontent.com/bunkahle/Transcrypt-Examples/master/README.md", False);
xmlhttp.send()
console.log(xmlhttp.responseText)有关这方面的更多信息,请访问:
http://www.transcrypt.org/docs/html/special_facilities.html#creating-javascript-objects-with-new-constructor-call
https://stackoverflow.com/questions/48847562
复制相似问题