This article mainly introduces the getJSON cross-origin SyntaxError problem analysis. For more information, see
This article mainly introduces the getJSON cross-origin SyntaxError problem analysis. For more information, see
Yesterday I wrote a function: click "Mobile Phone Verification" to get the json data.
The javascript code is as follows:
$ (". Check_mobile"). click (function () {var mobile =$ ('. mobile'). val (); $. getJSON ("http://www.test.com/user.php? Mobile = "+ mobile +" & format = json & jsoncallback =? ", Function (data) {if (data. succ = 1) {var html =""; $ (". R_m "). append (html );}});});
The user. php code is as follows:
<? Phpif ($ _ GET) {$ mobile =$ _ GET ['mobile']; if ($ mobile = 'xxxx ') {$ user = array ('city' => 'shijiazhuang ', 'cityid' => '1', 'communi' => 'zijing yuecheng ', 'communityid' => '1'); $ sucess = 1; $ return = array ('succ' => $ sucess, 'data' => $ user );} else {$ sucess = 2; $ return = array ('succ' => $ sucess);} echo json_encode ($ return) ;}?>
The response is as follows:
The problem arises:
In Firefox: SyntaxError: missing; before statement
The solution is as follows:
Header ("Access-Control-Allow-Origin: http:"); $ B = json_encode ($ return ); echo "{$ _ GET ['jsoncallback']} ({$ B})"; exit;
Complete code:
<? Phpheader ("Access-Control-Allow-Origin: http:"); if ($ _ GET) {$ mobile =$ _ GET ['mobile']; if ($ mobile = '000000') {$ user = array ('city' => 'shijiazhuang ', 'cityid' => '1 ', 'comment' => 'zijing yuecheng ', 'communityid' => '1'); $ sucess = 1; $ return = array ('succ' => $ sucess, 'data' => $ user);} else {$ sucess = 2; $ return = array ('succ' => $ sucess );} $ B = json_encode ($ return); echo "{$ _ GET ['jsoncallback']} ({$ B})"; exit ;}?>
If the header ("Access-Control-Allow-Origin: http:") is missing in PHP
XMLHttpRequest cannot load ''. No 'access-Control-Allow-origin' header is present on the requested resource. origin'' is therefore not allowed Access.
If echo "{$ _ GET ['jsoncallback']} ({$ B})" is missing
In Google's browser: Uncaught SyntaxError: Unexpected token:
In Firefox: SyntaxError: missing; before statement