在php中加了头还是不管用,前台代码运行在node上,端口8080,apache 3000
post变成options了;chrome中报这样的错
XMLHttpRequest cannot load http://localhost:3000/test.php. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
request头
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:zh-CN,zh;q=0.8
Access-Control-Request-Headers:access-control-allow-origin, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:3000
Origin:http://localhost:8080
Referer:http://localhost:8080/index
下面是php
<?php
header('Content-Type:application/json;charset=utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET,POST');
$backValue=$_POST['trans_data'];
echo $backValue."+后台返回";
?>这是ajax
$.support.cors =true;
var my_data="前台变量";
my_data=escape(my_data)+"";//编码,防止汉字乱码
$.ajax({
url: "http://localhost:3000/test.php",
type: "POST",
data:{trans_data:my_data},
//dataType: "jsonp",
contentType:"application/json",
crossDomain:true,
error: function(e){
console.log(e)
},
success: function(data,status){//如果调用php成功
alert(unescape(data));//解码,显示汉字
},
headers:{
'Access-Control-Allow-Origin':'http://localhost:8080'
}
})求解答啊~~~