XMLHttpRequest cannot load http://localhost/test01/serverjson.php?number=1. No 'Access-Control-Allow-Origin' header is present on the requested resource..
XMLHttpRequest cannot load http://localhost/test01/serverjson.php?number=1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. 出現(xiàn)這樣的錯誤,我查了一下是跨域的問題,但還在解決中。具體是什么還沒搞懂,有知道幫忙回我一下,謝謝!
2016-12-25
跨域的話,瀏覽器會先發(fā)一個options請求確定權(quán)限之類的,比如你是localhost:8011 server在localhost:8012
那么你就需要在server端響應(yīng)這個請求,如下:
response.setHeader("Access-Control-Allow-Origin", "host+端口號");
response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
response.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
來告訴瀏覽器他可以那么做,然后瀏覽器再進行g(shù)et/post請求