2016-11-24 57 views
1

我的挑戰是將我的cookie與我的有效載荷一起發送。我只是將一個cookie添加到現有的和正在運行的程序中。有效載荷(歌曲)到達那裏就好了。而且,我在瀏覽器控制檯沒有收到任何錯誤。瀏覽器沒有得到我的python服務器cookie?

我似乎能夠發送一個cooke,以及我的JSON數據就好了。

當我查看Chrome開發人員工具中的標題時,我看到了cookie。但是,當我在應用程序>> Cookie選項卡中查找cookie時,沒有任何內容。所以,cookie頭似乎被髮送,客戶端並沒有存儲它。

而且,在客戶端.... document.cookie爲空。

我的測試代碼正在創建一個名爲sessionID的cookie。

這裏是響應頭在Chrome中:你可以看到我的假會話ID的Cookie:

Access-Control-Allow-Credentials:true 

Access-Control-Allow-Origin:null 

Content-Type:text/plain 

Date:Wed, 23 Nov 2016 22:41:10 GMT 

Server:BaseHTTP/0.6 Python/3.5.2 

Set-Cookie:sessionID=4jjdd7ghhq 

這裏是指示不再餅乾截圖。 no cookies in application

這裏是我的服務器代碼,寫在Python3:

def do_OPTIONS(self): 
    self.send_response(200)  
    self.send_header("Access-Control-Allow-Origin", self.headers["Origin"]) 
    self.send_header("Access-Control-Allow-Credentials", "true")    
    self.send_header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') 
    self.send_header('Access-Control-Allow-Headers', 'Content-Type') 
    self.end_headers() 
    return 

def do_GET(self): 
    if self.path.startswith("/songs/ID"): 
     self.load_cookie() 
     db=MediaDB() 
     ID = self.parseID(self.path) 
     song = db.getSong(ID) 
     self.send_response(200) 
     self.send_header('Access-Control-Allow-Origin', self.headers["Origin"]) 
     self.send_header("Access-Control-Allow-Credentials", "true") 
     self.send_header("Content-Type", "text/plain")  
     self.cookie["sessionID"] = "4jjdd7ghhq" 
     self.send_cookie() 
     self.end_headers() 
     json_string = json.dumps(song) 
     self.wfile.write(bytes(json_string, "utf-8")) 
    elif self.path.startswith("/songs"): 
     self.getSongs() 
     return 
    else: 
     self.badPath() 
     return 


def load_cookie(self): 
    if "Cookie" in self.headers: 
     self.cookie = cookies.SimpleCookie(self.headers["Cookie"]) 
    else: 
     self.cookie = cookies.SimpleCookie() 
    return 

def send_cookie(self): 
    for morsel in self.cookie.values(): 
     self.send_header("Set-Cookie", morsel.OutputString()) 
    return 

================ 

我的搜索想出短。如果有什麼東西可以幫助我,我感謝你指出了這一點。哦,順便說一句,我的皮膚非常厚,所以如果我做了一些愚蠢的事情 - 可以指出來......我總得以某種方式學習。哈!

回答

0

默認情況下,當使用本地文件處理Chrome時,Chrome不支持cookie,正如我在您發佈的屏幕截圖中看到的那樣。

但是,您可以通過使用--enable-file-cookies標誌啓動Chrome來更改此設置。

查看更多about in this post

+0

我的Chrome能夠支持本地cookie,我有一個cookie測試,在沒有添加JSON負載的情況下工作得很好。但是,我使用了上面的-enable-file-cookies,但仍然不適合我。謝謝。 – Iggy

相關問題