JSONP cross-domain Request data Error "unexpected token:" Solution

Source: Internet
Author: User

Local virtual two domain names, respectively: www.test.com, www.abc.com

Http://www.test.com/index.html page Click button, request to return the data of the file under the www.abc.com domain name directory , the code is:

<! DOCTYPE html>


The http://www.abc.com/json.php file code is:

<?php $arr = Array (' username ' = ' jack ', ' age ' =>21, ' gender ' = ' male '); echo Json_encode ($arr);? >


Debugging in the Chrome browser will find an error,

The data returned by json.php is indeed a JSON-type data {"username": "Jack", "Age": $, "gender": "Male"}, where is the problem?

Look at it. jquery Document Discovery Jsonp: "Callback", Jsonpcallback: "Success_jsonpcallback", pass these two parameters for a reason, JSONP the return data format should be: " The client passes the callback method name (JSON data) and changes the PHP file to:

<?php $arr = Array (' username ' = ' jack ', ' age ' =>21, ' gender ' = ' male '); Echo $_get[' callback ']. " (". Json_encode ($arr).") ";? >


Test, return the results correctly, such as:


As you can see, the result of the PHP file return is Success_jsonpcallback ({"username": "Jack", "age": +, "gender": "Male"}), which is the correct JSONP return format, and Success_jsonpcallback This is the pass-through parameter .


JSONP cross-domain Request data Error "unexpected token:" Solution

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.