ajax跨域的一些基本错误

DATE: 2017-01-16 / VIEWS: 1403

如下:

XMLHttpRequest cannot load http://xxxx/xxx.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:8888' is therefore not allowed access.

又或:

XMLHttpRequest cannot load http://xxxx/xxx.php. Method DELETE(PUT) is not allowed by Access-Control-Allow-Methods in preflight response.

再或:

XMLHttpRequest cannot load http://xxxx/xxx.php. Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.


PHP解决办法如下:

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Content-Type");
header('Access-Control-Allow-Methods: GET,PUT, DELETE');

前台取到的数据不是JSON格式?

header('Content-type:application/json');