2012-04-01 51 views
7
url=require('url'); 

qs=require('querystring'); 

var http=require('http'); 

http.createServer(server).listen(1337, 'hostname'); 

function server(req, res) { 

    res.writeHead(200, {'Content-Type': 'text/plain'}); 

    res.write(req.url); 

    a=url.parse(req.url, true); 

    console.log(a); 

    res.end('\nHello World\n'); 

} 

console.log('Server running at http://127.0.0.1:1337/'); 




//http://host:1337/#A=1111111 <--- not coming in log or url 
//http://host:1337/?A=11111111 <--- works ok 

//usecase : facebook access_token url format is something similar to above 
+1

當你請求一個像這樣的URL:http:// host:1337 /#A = 1111111它只是發送這個到服務器http://主機:1337 /和這個片段到瀏覽器#A = 1111111 Facebook在本地獲取此網址在瀏覽器中,解析它,它變成像這樣的HTTP GET一個變量://主機:1337/A = 11111111 然後使用AJAX數據請求,分析所有數據,並將其寫入了?瀏覽器。 這是一個需要'onhashchange'事件的舊技術,目前,最好的方法是實現pushState並更改整個URL而不刷新頁面。 – neojp 2012-07-26 21:22:04

回答

19

'#'標記之後的部分URL(稱爲fragment)未發送到服務器。如果將數據存儲在片段中,則由您來處理該數據,並使用GET參數中的數據執行ajax請求。

+2

Facebook使用http:// servername:port/path#access_token = somestring – 2012-04-02 05:02:59

+0

以該格式回撥dev服務器我真的希望他們沒有關閉這個問題。這個facebook片段真的很煩人。 – Gavin 2014-06-20 00:25:56