This article mainly introduces the problem analysis of getJSON cross-origin SyntaxError. if you need this feature, you can refer to the function you wrote yesterday: click mobile phone verification to obtain json-side 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:www.test.com");$b = json_encode($return);echo "{$_GET['jsoncallback']}({$b})";exit;
Complete code:
<? Phpheader ("Access-Control-Allow-Origin: http: www.test.com"); 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: www.test.com") 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