使用XMLHTTPRequest(TVMLJS)发送cookie [英] send a cookie with XMLHTTPRequest (TVMLJS)

查看:427
本文介绍了使用XMLHTTPRequest(TVMLJS)发送cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为AppleTV开发一个应用程序.该应用程序将从尚未开发此类API的在线网站上读取电影.

我使用XMLHTTPRequest获取不同的URL,并让用户搜索他的电影等.除了一个请求,一切都正常.要获取电影URL,我必须向特定地址发送获取请求(例如 http://example.com/getmovie.html )和一个常量Cookie(例如mycookie = cookie).

我尝试使用setRequestHeader:

var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.withCredentials = true;
xhr.setRequestHeader('Cookie', 'mycookie=cookie');
xhr.send();

但是似乎没有发送任何cookie. 我还尝试使用Document.cookie设置cookie,就像我可能会在正常" js脚本中运行(在浏览器中运行)一样,但是也没有运气.

这非常令人沮丧,尤其是因为我紧贴应用程序的末尾.

我猜可能是跨域的问题,但是如果我不必设置cookie,我就能获得没有问题的URL,所以我有点迷路了.

请让我知道如何通过以下方式获取 http://example.com/getmovie.html 一个特定的Cookie标头.

感谢您的帮助

解决方案

很抱歉通知您,但出于安全原因,javascript的xmlHTTPRequest函数不允许设置cookie标头,如下所示:http://example.com/getmovie.html) with a constant cookie (let's say mycookie=cookie).

I've tried using setRequestHeader:

var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.withCredentials = true;
xhr.setRequestHeader('Cookie', 'mycookie=cookie');
xhr.send();

But no cookie seems to be sent. I also tried setting the cookie with Document.cookie like I would have probably done in a "normal" js script (running in my browser) but no luck either.

This is extremely frustrating, especially since I'm stuck so close to the end of my app.

I guess cross-origin might be the issue but I'm able to get URLs without issues if I don't have to set cookies, so I am a bit lost there.

Please let me know how I can get http://example.com/getmovie.html with a specific cookie header.

Thanks for your help

解决方案

im sorry to inform you but the xmlHTTPRequest function of javascript does not allow a cookie header to be set for security reasons as shown here: Why cookies and set-cookie headers can't be set while making xmlhttprequest using setRequestHeader? the best way i could see you making that get request would be to a proxy server that you would be running. I believe that it is built this way to prevent you from setting cookies on domains that you do not own, furthermore i do not see an alternate resolution to this problem as no were in the docs i looked at was cookie persistence or management mentioned

这篇关于使用XMLHTTPRequest(TVMLJS)发送cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆