在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)+"";//編碼,防止?jié)h字亂碼 $.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){//如果調(diào)用php成功 ????????alert(unescape(data));//解碼,顯示漢字 ????}, ????headers:{ ????????'Access-Control-Allow-Origin':'http://localhost:8080' ????} })
求解答啊~~~
2016-11-11
解決了,在apache中配置header,content-type有限制