AJax¼¼Êõ

ajax֪ʶ AjaxѧϰȫÌ×(×îÈ«×î¾­µä)(3)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2016-12-25 11:00 ÎÒÒªÆÀÂÛ( )

aylin.comÓòÃûÕâ±ß¿ÉÒÔ¸ø¶¨Ò庯Êý # ²ÉÓÃpythontornado¿ò¼ÜÀ´½øÐеÄclass IndexHandler(tornado.web.RequestHandler): def get(self): self.write('func([11,22,33]);') def post(self, *args, **kwargs): self.wri

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

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • Java EE£ºAjax¼¼ÊõÔÚÄÄÀ

    Java EE£ºAjax¼¼ÊõÔÚÄÄÀ

    2016-12-25 12:08

  • ¡¾Ajax¡¿¡ª¡ªÑ§Ï°ºóµÄС½á

    ¡¾Ajax¡¿¡ª¡ªÑ§Ï°ºóµÄС½á

    2016-12-25 10:05

  • J2EE¿ª·¢¼¼Êõµã4£ºajax¼¼Êõ

    J2EE¿ª·¢¼¼Êõµã4£ºajax¼¼Êõ

    2016-12-24 17:00

  • AJAX¼¼Êõ½â¶Á

    AJAX¼¼Êõ½â¶Á

    2016-12-24 16:08

ÍøÓѵãÆÀ