非常简单!!加入返回结果时加入以下代码,完美解决!
header('content-type:application:json;charset=utf8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Methods:GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
示例:
<?php
if (isset($_GET['name'])) {
require_once '../mysql/mysql_connect.php';
$name = escape_data($_GET['name']);
$level = escape_data($_GET['level']);
$sql = "insert into rank(name,level,publish_date)values('$name','$level',NOW())";
$result = mysql_query($sql);
if (mysql_affected_rows() == 1) {
$response = "".mysql_insert_id();
}else {
$response = 'false';
}
mysql_close();
header('content-type:application:json;charset=utf8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Methods:GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
echo $response;
}
?>
本文介绍了一种在PHP中实现跨域请求的方法,并通过设置HTTP头部信息来正确返回JSON格式的数据。文中给出的示例代码展示了如何在插入数据到数据库后返回相应的响应结果。
2235

被折叠的 条评论
为什么被折叠?



