$. Several Problems Encountered by getjson-output with HTML tags in JSON returned data

Source: Internet
Author: User
ArticleDirectory
  • Same domain:
  • Cross-origin:
1. Invalid label Error

There are two scenarios:

  • Same domain
  • Cross-Origin
Same domain:

Use eval to translate the returned JSON into response

VaR JSON = eval ('+ myjson + ')');
Cross-origin:
First, the URL has callback =?
Second, we also need to output the callback value during the output.
$ Dal = $ _ Get ['callback'];
Echo $ Dal. '('. json_encode ($ retdata1 ).')';
Note: When $ dal is connected to a JSON array, the JSON array must contain brackets.

 

2. Output JSON data with HTML tags

If you want to include the HTML Tag value in JSON data, it is the cause of failure during execution:

Because/is processed as \/, the browser determines that the tag is incomplete and the tag is automatically filled up, resulting in additional tags.

 

Solution:

$ JSON = json_encode ($ result );
$ Myjson = strtr ($ JSON, array ('<' => '\ u003c', ">" => '\ u003e '));

Echo $ myjson;

If phpversion> 5.3.0, You can echo json_encode ($ A, json_hex_tag );

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.