javascript 是否可以在服务器端运行 jQuery?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15109465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to run jQuery on server side?
提问by Subodh Ghulaxe
I am working on web page scraping has AJAX pagination's implemented, Since website is developed in asp i.e. page with extension .aspx I have tried submitting pagination forms to get data from other pages than first page but did not get any success, take a look here code I have used Scraping data from all asp.net pages with AJAX pagination implemented, So my question is how can I click on pagination links from php i.e. is it possible to run jQuery or javascript on server side?
我正在处理网页抓取并实现了 AJAX 分页,由于网站是在带有扩展名 .aspx 的 asp ie 页面中开发的,我尝试提交分页表单以从除第一页以外的其他页面获取数据,但没有取得任何成功,请看这里代码我使用了从所有已实现 AJAX 分页的 asp.net 页面中抓取数据,所以我的问题是如何单击来自 php 的分页链接,即是否可以在服务器端运行 jQuery 或 javascript?
I know Node.js can be used to run javascript on server side, but don't know how to use it with Apache and PHP
我知道 Node.js 可用于在服务器端运行 javascript,但不知道如何将它与 Apache 和 PHP 一起使用
采纳答案by Derrick Tucker
JavaScript can, in fact, be run on the server side.
事实上,JavaScript 可以在服务器端运行。
Your solution here, though, would be to use AJAX to call a PHP script for pagination, as server side javascript doesn't make much sense for the issue.
不过,您在这里的解决方案是使用 AJAX 调用 PHP 脚本进行分页,因为服务器端 javascript 对这个问题没有多大意义。
回答by Aleph Aleph
Use PhantomJSfor interacting with HTML pages on other services. There are libraries for PHP like this.
使用PhantomJS与其他服务上的 HTML 页面交互。有对PHP库像这样。
CasperJSis a library built on top of PhantomJS that allows to automate some common testing / scripting tasks.
CasperJS是一个建立在 PhantomJS 之上的库,它允许自动化一些常见的测试/脚本任务。
If you don't need to interact with remote pages but want to extract data from HTML using jQuery selectors, use Cheerio.
如果您不需要与远程页面交互但想要使用 jQuery 选择器从 HTML 中提取数据,请使用Cheerio。
回答by Dipesh Parmar
What you can do is
你能做的是
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function()
{
$('ul li a').on('click',function()
{
var href = $(this).attr('href');
$.ajax(
{
url : href,
data : passData,
dataType : 'html'
beforeSend : function()
{
},
success : function(response)
{
//display it as needed...
}
});
});
});
</script>
<ul>
<li><a href="paging.aspx?page=1">1</a></li>
<li><a href="paging.aspx?page=2">2</a></li>
<li><a href="paging.aspx?page=3">3</a></li>
<li><a href="paging.aspx?page=4">4</a></li>
</ul>
In example above I have four pagination link and on that links click event I make AJAX call and then in AJAX success call back display result as your wish.
在上面的示例中,我有四个分页链接,在该链接的单击事件中,我进行了 AJAX 调用,然后在 AJAX 成功回调中按照您的意愿显示结果。
But remember jQuery can't work on server use Node.js.
但是请记住 jQuery 不能在服务器使用 Node.js 上工作。
回答by cjm2671
I tried to do this, and it was painful.
我试图这样做,但很痛苦。
My recommendation (in the year 2020), especially if you're using React, is to use this: https://cheerio.js.org/.
我的建议(在 2020 年),特别是如果你使用 React,是使用这个:https: //cheerio.js.org/。
回答by Husman
No it is not possible to run jQuery on the serverside, as jQuery runs in the users web browser (inside the javascript interpreter) . You can make an Ajax call via jQuery to a PHP script which can run code on the server.
不,不可能在服务器端运行 jQuery,因为 jQuery 在用户 Web 浏览器中运行(在 javascript 解释器内)。您可以通过 jQuery 对 PHP 脚本进行 Ajax 调用,该脚本可以在服务器上运行代码。
回答by CR41G14
JQuery and Javascript is a client side language and for the pagination this can be easily achieved using jQuery as it should be.
JQuery 和 Javascript 是一种客户端语言,对于分页,这可以使用 jQuery 轻松实现,因为它应该是。
Ajax can be used to communicate with the server without page rendering and can give the user a better "Client side" experience. You can use Ajax to connect to the server and use jQuery to perform actions the return from the server.
Ajax 可用于与服务器通信,无需页面渲染,可为用户提供更好的“客户端”体验。您可以使用 Ajax 连接到服务器并使用 jQuery 执行从服务器返回的操作。