GetJSON cross-origin SyntaxError Analysis

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.