XmlSendRequest(){ XMLHttpRequest(); xhr.onreadystatechange = function(){ ) { xhr.responseText; console.log(result); } }; xhr.open(); xhr.send(); } function JqSendRequest(){ $.ajax({ url: , type: , dataType: , success: function(data, statusText, xmlHttpRequest){ console.log(data); } }) }
HTML
class MainHandler(tornado.web.RequestHandler): def get(self): self.set_header('Access-Control-Allow-Origin', "http://www.xxx.com") self.write('{"status": true, "data": "seven"}')
tornado2、支持跨域,复杂请求
由于复杂请求时,首先会发送“预检”请求,如果“预检”成功,则发送真实数据。
XmlSendRequest(){ XMLHttpRequest(); xhr.onreadystatechange = function(){ ) { xhr.responseText; console.log(result); } }; xhr.open(); xhr.setRequestHeader(); xhr.send(); } function JqSendRequest(){ $.ajax({ url: , type: , dataType: , headers: {}, success: function(data, statusText, xmlHttpRequest){ console.log(data); } }) }
HTML
class MainHandler(tornado.web.RequestHandler): def put(self): self.set_header('Access-Control-Allow-Origin', "http://www.xxx.com") self.write('{"status": true, "data": "seven"}') def options(self, *args, **kwargs): self.set_header('Access-Control-Allow-Origin', "http://www.xxx.com") self.set_header('Access-Control-Allow-Headers', "k1,k2") self.set_header('Access-Control-Allow-Methods', "PUT,DELETE") self.set_header('Access-Control-Max-Age', 10)
tornado3、跨域传输cookie
在跨域请求中,默认情况下,HTTP Authentication信息,Cookie头以及用户的SSL证书无论在预检请求中或是在实际请求都是不会被发送。
如果想要发送:
XmlSendRequest(){ XMLHttpRequest(); xhr.onreadystatechange = function(){ ) { xhr.responseText; console.log(result); } }; xhr.withCredentials = true; xhr.open(); xhr.setRequestHeader(); xhr.send(); } function JqSendRequest(){ $.ajax({ url: , type: , dataType: , headers: {}, xhrFields:{withCredentials: true}, success: function(data, statusText, xmlHttpRequest){ console.log(data); } }) }
HTML
class MainHandler(tornado.web.RequestHandler): def put(self): self.set_header('Access-Control-Allow-Origin', "http://www.xxx.com") self.set_header('Access-Control-Allow-Credentials', "true") self.set_header('xxoo', "seven") self.set_header('zhangyanlinhenshuai', "feichangshuai") self.set_header('Access-Control-Expose-Headers', "shuai,shuaishuai") self.set_cookie('kkkkk', 'vvvvv'); self.write('{"status": true, "data": "seven"}') def options(self, *args, **kwargs): self.set_header('Access-Control-Allow-Origin', "http://www.xxx.com") self.set_header('Access-Control-Allow-Headers', "k1,k2") self.set_header('Access-Control-Allow-Methods', "PUT,DELETE") self.set_header('Access-Control-Max-Age', 10)
View Code