创建start.py,代码如下:

    1. #!/usr/bin/env python
    2. try:
    3. # Python 3
    4. from http.server import HTTPServer, SimpleHTTPRequestHandler, test as test_orig
    5. import sys
    6. def test (*args):
    7. test_orig(*args, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)
    8. except ImportError: # Python 2
    9. from BaseHTTPServer import HTTPServer, test
    10. from SimpleHTTPServer import SimpleHTTPRequestHandler
    11. class CORSRequestHandler (SimpleHTTPRequestHandler):
    12. def end_headers (self):
    13. self.send_header('Access-Control-Allow-Origin', '*')
    14. SimpleHTTPRequestHandler.end_headers(self)
    15. if __name__ == '__main__':
    16. test(CORSRequestHandler, HTTPServer)

    运行start.py,端口为8000

    1. python start.py 8000

    然后js请求就不存在跨域问题了666