PHP Background code solves cross-domain issues

Source: Internet
Author: User

In the front-end, to solve the cross-domain time always seem so disgusting, what Jsonp Ah, Ajax ah, cors ah what, always feel is in the loopholes in the cross-domain, in fact, in PHP file only need to add a piece of code can cross the domain, the front-end you how to write or how to write, Post,get casually use:

Header ("access-control-allow-origin:*");


Header ("access-control-allow-origin:*"); Add this sentence to achieve the front-end cross-domain function GetKey ($key, $default = "") {    return trim (Isset ($_request[$key])? $_request[$key]:$ default);} $id = GetKey ("id"), $conn = Mysqli_connect ("localhost", "root", "" "," test ") or Die (" Connection Failed "), $conn->query (" Set names UTF8 $sql = "SELECT * from Data where". $id. ' is not null '; $result = $conn->query ($sql); $arr = [];while ($row = $result FETCH_ASSOC ()) {    Array_push ($arr, Json_encode ($row));} $json = Json_encode ($arr);  JSON data Print_r ($json);

  

PHP Background code solves cross-domain issues

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.