aylin.comÓòÃûÕâ±ß¿ÉÒÔ¸ø¶¨Ò庯Êý
# ²ÉÓÃpythontornado¿ò¼ÜÀ´½øÐеÄclass IndexHandler(tornado.web.RequestHandler): def get(self): self.write('func([11,22,33]);') def post(self, *args, **kwargs): self.write('t2.post')ÔÚÕâÀïjsonp¾Í²ÉÓÃscript±êÇ©µÄsrcÀ´½øÐпçÓòÇëÇóµÄ
¶þ¡¢CORS
ÉÏÃæÄÇÖÖ·½·¨Ëµµ½ä¯ÀÀÆ÷µÄͬԴ²ßÂÔµ¼ÖÂajaxÎÞ·¨½øÐпçÓò´«Ê䣬ÄÇôÕâÖÖ·½·¨¾Í¿ÉÒÔÍ»ÆÆä¯ÀÀÆ÷ÏÞÖÆÀ´½øÐд«Êä¡£µ±Êý¾Ý·¢Ë͸ø¶Ô·½ÓòÃûµÄʱºò£¬¶Ô·½ÒѾÊÕµ½£¬µ«ÊÇÔÚ·µ»ØµÄʱºò±»ä¯ÀÀÆ÷¸ø×èµ²£¬ÎÒÃÇ¿ÉÒÔдһ´®ÀàËÆÓÚÉí·ÝÖ¤µÄ×Ö·û´®£¬Í¨¹ýä¯ÀÀÆ÷µÄÔ¤¼ì£¬´Ó¶ø´ïµ½Êý¾ÝµÄ´«Êä¡£
Õâ·½Ãæ·ÖΪ¼òµ¥ÇëÇóºÍ·Ç¼òµ¥ÇëÇó
Ìõ¼þ£º 1¡¢ÇëÇó·½Ê½£ºHEAD¡¢GET¡¢POST 2¡¢ÇëÇóÍ·ÐÅÏ¢£º Accept Accept-Language Content-Language Last-Event-ID Content-Type ¶ÔÓ¦µÄÖµÊÇÒÔÏÂÈý¸öÖеÄÈÎÒâÒ»¸ö application/x-www-form-urlencoded multipart/form-data text/plain×¢Ò⣺ͬʱÂú×ãÒÔÉÏÁ½¸öÌõ¼þʱ£¬ÔòÊǼòµ¥ÇëÇ󣬷ñÔòΪ¸´ÔÓÇëÇó¼òµ¥ÇëÇóÖ»Ò»´ÎÇëÇ󣬶ø¸´ÔÓÇëÇóÊÇÁ½´ÎÇëÇó£¬ÔÚ·¢ËÍÊý¾Ý֮ǰ»áÏÈ·¢Ò»´ÎÇëÇóÓÃÓÚ×ö“Ô¤¼ì”£¬Ö»ÓГԤ¼ì”ͨ¹ýºó²ÅÔÙ·¢ËÍÒ»´ÎÇëÇóÓÃÓÚÊý¾Ý´«Êä¡£
»ùÓÚcorsʵÏÖAJAXÇëÇó:
1¡¢Ö§³Ö¿çÓò£¬¼òµ¥ÇëÇó
·þÎñÆ÷ÉèÖÃÏìӦͷ£ºAccess-Control-Allow-Origin = 'ÓòÃû' »ò '*'
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title></head><body> <p> <input type="submit" onclick="XmlSendRequest();" /> </p> <p> <input type="submit" onclick="JqSendRequest();" /> </p> <script type="text/javascript" src="jquery-1.12.4.js"></script> <script> function XmlSendRequest(){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4) { var result = xhr.responseText; console.log(result); } }; xhr.open('GET', ":8000/test/", true); xhr.send(); } function JqSendRequest(){ $.ajax({ url: ":8000/test/", type: 'GET', dataType: 'text', success: function(data, statusText, xmlHttpRequest){ console.log(data); } }) } </script></body></html> class MainHandler(tornado.web.RequestHandler): def get(self): self.set_header('Access-Control-Allow-Origin', "") self.write('{"status": true, "data": "seven"}')2¡¢Ö§³Ö¿çÓò£¬¸´ÔÓÇëÇó
ÓÉÓÚ¸´ÔÓÇëÇóʱ£¬Ê×ÏȻᷢËÍ“Ô¤¼ì”ÇëÇó£¬Èç¹û“Ô¤¼ì”³É¹¦£¬Ôò·¢ËÍÕæʵÊý¾Ý¡£
•“Ô¤¼ì”ÇëÇóʱ£¬ÔÊÐíÇëÇó·½Ê½ÔòÐè·þÎñÆ÷ÉèÖÃÏìӦͷ£ºAccess-Control-Request-Method
•“Ô¤¼ì”ÇëÇóʱ£¬ÔÊÐíÇëÇóÍ·ÔòÐè·þÎñÆ÷ÉèÖÃÏìӦͷ£ºAccess-Control-Request-Headers
•“Ô¤¼ì”»º´æʱ¼ä£¬·þÎñÆ÷ÉèÖÃÏìӦͷ£ºAccess-Control-Max-Age
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title></head><body> <p> <input type="submit" onclick="XmlSendRequest();" /> </p> <p> <input type="submit" onclick="JqSendRequest();" /> </p> <script type="text/javascript" src="jquery-1.12.4.js"></script> <script> function XmlSendRequest(){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4) { var result = xhr.responseText; console.log(result); } }; xhr.open('PUT', ":8000/test/", true); xhr.setRequestHeader('k1', 'v1'); xhr.send(); } function JqSendRequest(){ $.ajax({ url: ":8000/test/", type: 'PUT', dataType: 'text', headers: {'k1': 'v1'}, success: function(data, statusText, xmlHttpRequest){ console.log(data); } }) } </script></body></html> class MainHandler(tornado.web.RequestHandler): def put(self): self.set_header('Access-Control-Allow-Origin', "") self.write('{"status": true, "data": "seven"}') def options(self, *args, **kwargs): self.set_header('Access-Control-Allow-Origin', "") 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)3¡¢¿çÓò´«Êäcookie
ÔÚ¿çÓòÇëÇóÖУ¬Ä¬ÈÏÇé¿öÏ£¬HTTP AuthenticationÐÅÏ¢£¬CookieÍ·ÒÔ¼°Óû§µÄSSLÖ¤ÊéÎÞÂÛÔÚÔ¤¼ìÇëÇóÖлòÊÇÔÚʵ¼ÊÇëÇó¶¼ÊDz»»á±»·¢ËÍ¡£
Èç¹ûÏëÒª·¢ËÍ£º
•ä¯ÀÀÆ÷¶Ë£ºXMLHttpRequestµÄwithCredentialsΪtrue
•·þÎñÆ÷¶Ë£ºAccess-Control-Allow-CredentialsΪtrue
¡¡