Getjson cross-domain syntaxerror problem analysis, getjsonsyntaxerror_php tutorial

Source: Internet
Author: User

Getjson cross-domain syntaxerror problem analysis, Getjsonsyntaxerror


Write a function yesterday: Click on the phone to verify the data at the same time to get the JSON side.

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 ', ' Community ' + ' amethyst Yue City ', ' communityid ' = ' 1 '); $sucess = 1; $return = Array (' succ ' = $sucess, ' data ' = $user);} else {$sucess = 2; $return = Array (' succ ' = = $sucess);} echo Json_encode ($return);}? >

The corresponding following:

Here's the problem:

In the Firefox browser: syntaxerror:missing; Before statement

Here's how to fix it:

Header ("access-control-allow-origin:http:www.test.com"), $b = Json_encode ($return), echo ' {$_get[' Jsoncallback '}} ({ $b}) "; exit;

Final complete code:

<?phpheader ("access-control-allow-origin:http:www.test.com"), if ($_get) {$mobile = $_get[' mobile '];if ($mobile = = ' 18831167979 ' {$user = array (' City ' = ' Shijiazhuang ', ' cityid ' = ' 1 ', ' Community ' and ' Amethyst Yue ', ' 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 is missing from PHP ("access-control-allow-origin:http:www.test.com"); Code, you will receive

XMLHttpRequest cannot load '. No ' Access-control-allow-origin ' header is present on the requested resource. Origin ' is therefore not allowed access.
If you are missing echo ' {$_get[' Jsoncallback '} ({$b}) "; Code

In Google Chrome: uncaught syntaxerror:unexpected token:
In the Firefox browser: syntaxerror:missing; Before statement


Why do I use Getjson to cross the domain,

In the actual Web site application, pure JS to achieve cross-domain access should be said to be impossible, do not believe that some misleading online, the reason to emphasize is "in the actual use of the site" condition, is because IE browser will give local static web page higher permissions, can be asynchronous access to any site, But if you put it in the real domain of the site of the use of the application is not. Cross-domain is the need for server-side acting as a proxy, so the nature of cross-domain is actually "pseudo-cross-domain", this is just contact with Ajax friends have some misleading, I believe JS can cross-domain, then you go astray

jquery cross-domain access issues and the use of Getjson Ajax

Based on security considerations
This cross-domain is not possible with Jquery.
Getjson need to return callback

http://www.bkjia.com/PHPjc/860468.html www.bkjia.com true http://www.bkjia.com/PHPjc/860468.html techarticle Getjson cross-domain syntaxerror problem analysis, Getjsonsyntaxerror wrote a function yesterday: Click on the phone to verify the data at the same time to get the JSON side. 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.