thinkphpͼƬ¿çÓò²»ÏÔʾÎÊÌâÔõÑù½â¾ö
Ê×ÏÈ£¬ÈÃÎÒÃÇÏàʶ¿çÓòÊÇʲô¡£¿çÓò±¬·¢ÔÚÁ½¸ö²î±ðÓòÃûµÄÍøÕ¾Ö®¼äµ±Ò»¸öÍøÕ¾µÄ×ÊÔ´ÈçJS¡¢CSS¡¢Ajax»òÕßͼƬµÈÇëÇó»á¼ûÁíÒ»¸öÍøÕ¾µÄ×ÊԴʱ£¬ÓÉÓÚͬԴսÂÔµÄÏÞÖƶø±»×èÖ¹¡£ÏÞÖÆÒ»¸öÍøÕ¾Ö»ÄÜ»á¼ûÓëÆä×ÔÉíÓµÓÐÏàͬÓòÃû¡¢ÐæźͶ˿ڵÄ×ÊÔ´ÊÇͬԴսÂÔµÄÒ»¸öÒªº¦Çå¾²ÌØÕ÷¡£
ÎÒÃÇÔÚÏîÄ¿ÖÐʹÓÃÁËÒ»¸öµÚÈý·½¿âÀ´¼ÓÔØͼƬ£¬¸Ã¿â½ÓÄÉÁ˾ßÓвî±ðÓòÃûµÄµØµãÀ´´æ´¢Í¼Æ¬¡£Òò´Ë£¬ÔÚä¯ÀÀÆ÷ÖУ¬Í¼Æ¬¿çÓòʱÎÞ·¨Õý³£ÏÔʾ¡£
ΪÏàʶ¾öÕâ¸öÎÊÌ⣬ÎÒÃÇÐèÒªÔÚЧÀÍÆ÷¶Ë¾ÙÐÐһЩÉèÖá£×î³£¼ûµÄ½â¾öÒªÁìÊÇÔÚЧÀÍÆ÷¶ËÉèÖÃCORS£¨¿çÔ´×ÊÔ´¹²Ïí£©¡£CORSʹµÃЧÀÍÆ÷¿ÉÒÔÔÚÏìÓ¦¿Í»§¶ËÇëÇóʱ£¬ÏÞÖÆÖ»ÓÐÌض¨µÄÓòÃû¿ÉÒÔ»á¼û¸Ã×ÊÔ´¡£
ÔÚThinkPHP¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÔÚindex.phpÎļþÖÐÌí¼ÓÒÔÏ´úÂëÀ´ÊµÏÖCORS£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
header('Access-Control-Allow-Origin:*'); //ÔÊÐíËùÓÐȪԴ»á¼û header('Access-Control-Allow-Method:POST,GET'); //ÔÊÐí»á¼ûµÄ·½·¨
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂëÔÊÐíËùÓÐȪԴ»á¼û×ÊÔ´£¬²¢ÇÒÔÊÐíPOSTºÍGET·½·¨µÄ»á¼û¡£
ÔÚÕâÀïҪעÖصÄÊÇ£¬Õâ¶Î´úÂëÓ¦¸Ã·ÅÔÚindex.phpµÄ¶¥²¿£¬ÕâÑùËùÓеÄÇëÇó¶¼ÄÜʹÓøÃÉèÖá£
³ýÁËÉèÖÃCORS£¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃÆäËûÒªÁìÀ´½â¾ö¿çÓòÎÊÌâ¡£ÀýÈçJSONP£¨JSON with Padding£©£¬ËüÊÇÒ»ÖÖ½â¾ö¿çÓòµÄ¿ÉÐÐÒªÁ죬ͨ¹ýÔÚÒ³ÃæÖÐÌí¼ÓÒ»¸öscript±êÇ©£¬½«ÐèÒª»á¼ûµÄÊý¾Ý°ü×°³ÉÒ»¸öº¯Êý£¬È»ºóÔÚÁíÒ»¸öÍøÕ¾ÉÏÎüÊÕ·µ»ØµÄЧ¹û¡£
ÔÚThinkPHP¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏ´úÂëÀ´ÌìÉúJSONPÒªÁ죺
$callback = isset($_GET['callback']) ? $_GET['callback'] : 'callback'; $data = array('name' => 'test', 'age' => 18); echo $callback . '(' . json_encode($data) . ')';
µÇ¼ºó¸´ÖÆ
ÎÒÃÇÏÈÉó²éGET²ÎÊýÊÇ·ñÓÐcallback²ÎÊý£¬ÈôÓУ¬ÔòÓøòÎÊý×÷Ϊº¯ÊýÃû¡£½Ó×Å£¬½«Ðè·µ»ØµÄÊý¾Ýת»»³ÉJSONÃûÌã¬ÔÙ½«ÆäÌí¼Óµ½»Øµ÷º¯ÊýÖС£×îºó£¬ÔÚ·µ»ØÊý¾Ýʱ£¬ÎÒÃǽ«º¯ÊýºÍÊý¾ÝÒ»Æ𷵻ظøä¯ÀÀÆ÷¡£
ÒÔÉϾÍÊÇthinkphpͼƬ¿çÓò²»ÏÔʾÎÊÌâÔõÑù½â¾öµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡