如何通过 jsonp 读取 cookie?
How read cookies by jsonp?
您好,我有这段代码可以从 index.html:
加载页面
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.2.min.js"></script>
<script>
function jsonCallback(json){
console.log(json);
alert(document.cookie.json)
}
$.ajax({
url: "https://run.plnkr.co/plunks/v8xyYN64V4nqCshgjKms/data-2.json",
dataType: "jsonp"
});
</script>
我想要获取 cookie,所以在 https://run.plnkr.co/plunks/v8xyYN64V4nqCshgjKms/data-2.json 我在控制台中执行了:document.cookie='test=123'
和 return 未定义。为什么?我该如何解决?
这是 URL returns:
jsonCallback(
{
"sites":
[
{
"siteName": "SitePoint",
"domainName": "https://www.sitepoint.com",
"description": "SitePoint is a hub for web developers to share their passion for building incredible Internet things."
},
{
"siteName": "A List Apart",
"domainName": "http://alistapart.com/",
"description": "A List Apart explores the design, development, and meaning of web content, with a special focus on web standards and best practices."
},
{
"siteName": "Smashing Magazine",
"domainName": "https://www.smashingmagazine.com/",
"description": "Smashing Magazine delivers useful and innovative information to Web designers and developers."
}
]
}
);
您不能在您的页面中使用 JavaScript 代码来读取其他来源的 cookie。如果您有 http://example.com/foo.html
并在其中加载来自 http://api.com/
的脚本(这是您使用 JSONP 所做的),则来自 http://api.com/
的响应返回的任何 cookie 都与 http://api.com/
。您无法使用 http://example.com/foo.html
中的 JavaScript 代码阅读它们。
您好,我有这段代码可以从 index.html:
加载页面<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.2.min.js"></script>
<script>
function jsonCallback(json){
console.log(json);
alert(document.cookie.json)
}
$.ajax({
url: "https://run.plnkr.co/plunks/v8xyYN64V4nqCshgjKms/data-2.json",
dataType: "jsonp"
});
</script>
我想要获取 cookie,所以在 https://run.plnkr.co/plunks/v8xyYN64V4nqCshgjKms/data-2.json 我在控制台中执行了:document.cookie='test=123'
和 return 未定义。为什么?我该如何解决?
这是 URL returns:
jsonCallback(
{
"sites":
[
{
"siteName": "SitePoint",
"domainName": "https://www.sitepoint.com",
"description": "SitePoint is a hub for web developers to share their passion for building incredible Internet things."
},
{
"siteName": "A List Apart",
"domainName": "http://alistapart.com/",
"description": "A List Apart explores the design, development, and meaning of web content, with a special focus on web standards and best practices."
},
{
"siteName": "Smashing Magazine",
"domainName": "https://www.smashingmagazine.com/",
"description": "Smashing Magazine delivers useful and innovative information to Web designers and developers."
}
]
}
);
您不能在您的页面中使用 JavaScript 代码来读取其他来源的 cookie。如果您有 http://example.com/foo.html
并在其中加载来自 http://api.com/
的脚本(这是您使用 JSONP 所做的),则来自 http://api.com/
的响应返回的任何 cookie 都与 http://api.com/
。您无法使用 http://example.com/foo.html
中的 JavaScript 代码阅读它们。