码迷,mamicode.com
首页 > Web开发 > 详细

跨域http请求

时间:2015-09-01 10:30:16      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

$conn = new mysqli("myServer", "myUser", "myPassword", "Northwind");

$result = $conn->query("SELECT CompanyName, City, Country FROM Customers");

$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "") {$outp .= ",";}
    $outp .= ‘{"Name":"‘  . $rs["CompanyName"] . ‘",‘;
    $outp .= ‘"City":"‘   . $rs["City"]        . ‘",‘;
    $outp .= ‘"Country":"‘. $rs["Country"]     . ‘"}‘; 
}
$outp =‘{"records":[‘.$outp.‘]}‘;
$conn->close();

echo($outp);
?>

  关键是这句:

header("Access-Control-Allow-Origin: *");

跨域http请求

标签:

原文地址:http://www.cnblogs.com/codeAB/p/4774931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!