jQuery 效率低下?
我仍在学习 jQuery,我还没有弄清楚一些概念,所以如果这是一个非常菜鸟的问题,我很抱歉,但是当我通过以下方式从 PHP/MySQL 检索信息时遇到问题:
$.get('backend.php', {}, function (data) {
doSomething(data);
});
我不知道我是否做错了什么,但即使我在 index.php< 中使用
标记创建连接和会话/code>,当 jQuery 调用
backend.php
文件,其行为就像从未创建过一样。
每次我请求新数据时,是否都必须更新数据库连接并调用 session_start
?
I'm still learning jQuery and I haven't figured out some of the concepts yet, so I'm sorry if this is a very noob question, but I have a problem when retrieving information with jQuery from PHP/MySQL in the following manner:
$.get('backend.php', {}, function (data) {
doSomething(data);
});
I don't know if I'm doing something wrong, but even if I create the connection and the session with a <?php ?>
tag in the index.php
, when jQuery calls to the backend.php
file, it behaves like if they were never created.
Do I have to renew the database connection and call session_start
every single time I ask for the new data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你会的。在服务器上,每个页面调用都是对服务器的新页面调用。这就是您使用 cookie/令牌/其他内容来跟踪网站上用户的原因。
从好的方面来说,这与您可以拥有一个充满“哑”服务器的服务器场完全相同的原因,这些服务器将轮询您的用户,并且一切仍将“保留”。我强烈建议您在 PHP/Apache(服务器)上做一些工作,然后再切换到 jQuery/Ajax(客户端),因为您将更好地掌握什么是服务器端和什么是客户端。
You do. On the server every page-call is a new page-call to the server. That's the reason you use cookies/tokens/whatever to follow users across a site.
On the bright side that is exactly the same reason why you can have a farm full of "dumb" servers that will round-robin your users and everything will still "hold". I would strongly recomend you do some work on PHP/Apache (server) and only then switch to jQuery/Ajax (client) as you will get a stronger grasp of what's server-side and what's client-side.
您确实需要执行所有这些操作,但为了使事情变得更容易,您可以创建一个全局文件(例如
init.php
),该文件可以包含在您的所有脚本中,这样您就不必编写同样的事情一遍又一遍。You do need to do all that, but to make things easier, you can create a global file (e.g.
init.php
) that could be included in all your scripts so you don't have to write the same thing over and over.