0
我試圖通過XHR請求從瀏覽器控制檯發送JSON爲localhost哪裏放置我的handler.php 我XHR URL設置爲本地主機/ handler.php,但它不沒有用。它剛剛追加到當前網址的末尾。從Chrome中的控制檯發送XHR請求爲localhost WAMP
www.current-url.com/something/text/localhost/handler.php
我試圖通過XHR請求從瀏覽器控制檯發送JSON爲localhost哪裏放置我的handler.php 我XHR URL設置爲本地主機/ handler.php,但它不沒有用。它剛剛追加到當前網址的末尾。從Chrome中的控制檯發送XHR請求爲localhost WAMP
www.current-url.com/something/text/localhost/handler.php
網址101:如果你在一個網站「http://example.com/subdir」,那麼這裏的各種URL類型是如何被intrepreted:
relative: foo.html -> http://example/subdir/foo.html
relative: ../foo.html -> http://example.com/foo.html
relative: ../otherdir/foo.html -> http://example.com/otherdir/foo.html
local absolute: /foo.html -> http://examle.com/foo.html
local relative: google.com/hi/mom/foo.html -> http://example.com/subdir/hi/mom/foo.html
local absolute: /google.com/hi/mom/foo.html -> http://example.com/google.com/hi/mom/foo.html
universal absolute: http://google.com/kittens -> http://google.com/kittens
換句話說,如果你想解釋的URL作爲其他方面,它必須是「通用絕對」,幷包含完整的網址:protocol://host/path
。
謝謝,它工作:)但現在我得到不允許錯誤:XMLHttpRequest無法加載http://localhost/handler.php。請求的資源上沒有「Access-Control-Allow-Origin」標題。 Origin'http://example.com'因此不被允許訪問。你對這個問題有什麼想法嗎?我如何解決它? –
你不能對「外部」地址執行ajax請求,除非你設置了CORS頭,正如錯誤所述。 –
我該如何實現它以下面的代碼:var xmlhttp = new XMLHttpRequest(); xmlhttp.open(「POST」,「http://localhost/handler.php」); xmlhttp.setRequestHeader(「Content-Type」,「application/json; charset = UTF-8」); xmlhttp.send(json_data); –