GetJSON cross-origin SyntaxError problem analysis, getjsonsyntaxerror_PHP tutorial

Source: Internet
Author: User
GetJSON cross-origin SyntaxError analysis, getjsonsyntaxerror. GetJSON cross-origin SyntaxError problem analysis. getjsonsyntaxerror wrote a function yesterday: Click "mobile phone verification" to obtain json data at the same time. The javascript code is as follows: $ (. check_mobile). getJSON cross-origin SyntaxError problem analysis, getjsonsyntaxerror

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: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


Why can't I use getJSON for cross-origin ,,

In actual website use, it should be impossible to implement cross-origin access using pure js. do not trust the misleading information on the internet. the reason why it should be emphasized that it is a condition for "actual website use, the reason is that the IE browser will give the local static webpage higher permissions and can access any website asynchronously, but it will not work if you put it into a real website with domain ownership. Cross-origin requires the server to act as a proxy. Therefore, the nature of cross-origin is actually "pseudo-Cross-origin". This is misleading for friends who are new to ajax. I believe that js can be cross-origin, then you will go astray.

Cross-origin access of jquery and Ajax for getjson

Based on security considerations
Jquery cannot use this cross-origin method.
Call back must be returned for getjson.

Token wrote a function yesterday: Click "mobile phone verification" to obtain json-side data. The javascript code is as follows: $ (". check_mobile ")....

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.