可在头一个AJAX请求被发送? [英] Can headers be sent in an AJAX request?

查看:134
本文介绍了可在头一个AJAX请求被发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以调用服务器来设置一个AJAX请求一个新的Cookie(即,页面已经加载之后)?

Can I call the server to set a new cookie with an AJAX request (that is, after the page has already loaded)?

例如,当访问者点击一个链接,阿贾克斯将打开,设置这样一个新的Cookie的PHP文件:

For example, when a visitor hits a link, ajax would open a php file that sets a new cookie like this:

setcookie('cookiename', 'true', time()+3000, "/",'...');

但是,这是HTML完成后(包含实际的&LT页面;一> 标签pressed)被渲染。它是仍然确定设置阿贾克斯饼干? (也许是因为加载的PHP文件是独立于原始的HTML页面)。

But this is done after the html (the page containing the actual <a> tag pressed) was rendered. Is it nevertheless ok to set cookies in ajax? (maybe because the php file loaded is separate from the original html page).

推荐答案

您可以有服务器的响应设置一个cookie,当然。请记住,cookie是一个HTTP的事情,不是一个HTML的事情;你原来的HTML文件已经在浏览器上的事实是无关紧要的。您的Ajax请求是一个单独的HTTP请求到服务器,(希望!)产生一个HTTP响应返回给浏览器;这些响应包括一个新的设置Cookie 头。

You can have the server's response set a cookie, certainly. Remember that cookies are an HTTP thing, not an HTML thing; the fact that your original HTML file is already on the browser is irrelevant. Your ajax request is a separate HTTP request to the server, which (hopefully!) generates an HTTP response back to the browser; and that response can include a new Set-Cookie header.

我不是一个PHP的人,你需要检查是否有在你使用的设置cookie(我不能想象有)的PHP机制的限制。但是从根本上,没有,没有任何问题这样做。我已经与这两个JSP和传统的ASP做到了。

I'm not a PHP person, you'll need to check that there are limitations in the PHP mechanism you're using for setting the cookie (I can't imagine there are). But fundamentally, no, there's no problem doing this. I've done it with both JSPs and classic ASP.

这篇关于可在头一个AJAX请求被发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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