


GetJSON cross-domain SyntaxError problem analysis, getjsonsyntaxerror_PHP tutorial
getJSON cross-domain SyntaxError problem analysis, getjsonsyntaxerror
Yesterday I wrote a function: click on the mobile phone to verify and get the data on the json side at the same time.
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 = "<input type='hidden' name='cityid' value='"+data.data.cityid+"'><input type='hidden' name='communityid' value='"+data.data.communityid+"'>"; $(".r_m").append(html); } }); });
user.php code is as follows:
<?php if($_GET){ $mobile = $_GET['mobile']; if ($mobile == 'XXXX') { $user = array( 'city' =>'石家庄', 'cityid' =>'1', 'community' =>'紫晶悦城', 'communityid'=>'1' ); $sucess = 1; $return = array( 'succ' =>$sucess, 'data' => $user ); }else { $sucess = 2; $return = array( 'succ' =>$sucess ); } echo json_encode($return); } ?>
Correspondingly as follows:
The problem is:
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;
Final complete code:
<?php header("Access-Control-Allow-Origin:http:www.test.com"); if($_GET){ $mobile = $_GET['mobile']; if ($mobile == '18831167979') { $user = array( 'city' =>'石家庄', 'cityid' =>'1', 'community' =>'紫晶悦城', '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"); code is missing in PHP,
will appearXMLHttpRequest cannot load ''. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' ' is therefore not allowed access.
If there is missing echo "{$_GET['jsoncallback']}({$b})"; code
In Google Chrome: Uncaught SyntaxError: Unexpected token :
In Firefox: SyntaxError: missing ; before statement
In actual website use, it should be said that it is impossible to achieve cross-domain access with pure js. Don't believe some misleading information on the Internet. The reason why we emphasize the condition of "in actual website use" is because IE browser will give Local static web pages have higher permissions and can access any website asynchronously, but it will not work if you put it into a real domain-owned website application. Cross-domain requires the server to act as a proxy, so the essence of cross-domain is actually "pseudo-cross-domain", which is somewhat misleading for those who are new to ajax. If you believe that js can cross-domain, you will go astray.
Based on security considerations
Jquery cannot use this kind of cross-domain.
getjson needs to return callback

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.
