【发布时间】:2016-11-03 12:22:21
【问题描述】:
我已经通过 npm install 安装了 noodle.js,这就是我现在的代码。但是,当我在终端中运行此文件(noodleTest.js)时:node noodleTest.js,我收到以下错误:jQuery.getJSON 不是函数。我查看了有关此问题的大多数堆栈溢出答案,但找不到解决方法。通过http://noodlejs.com 提供的示例,它在控制台中返回一个数组,但是当我尝试运行这个 sn-p 时,我收到一个错误。任何帮助表示赞赏。
var noodle = require('noodlejs'),
jQuery = require ('jquery');
var query = {
url: 'http://google.com/search?q=javascript',
type: 'html',
selector: 'h3.r a',
extract: 'text'
},
uriQuery = encodeURIComponent(JSON.stringify(query)),
request = 'http://example.noodlejs.com/?q=' +
uriQuery + '&callback=?';
// Make Ajax request to Noodle server
jQuery.getJSON(request, function (data) {
console.log(data[0].results);
});
编辑:我尝试按照
http://%20github.com/dharmafly/noodle#noodle-as-a-node-module 给出的示例进行操作,并使用了在 noodlejs.com 上运行的示例,但我不确定如何读取数据,或如何提取对象中的内容。
var noodle = require('noodlejs');
noodle.query({
url: 'http://google.com/search?q=javascript',
selector: 'h3.r a',
extract: 'text'
})
.then(function (results) {
console.log(results);
});
这是以下输出:
[noodle log #1]
Thu Jul 07 2016 00:09:43 GMT-0700 (PDT)
Memory: 18.00mb (18870272 bytes)
Noodle: The query follows...
{"url":"http://google.com/search?q=javascript","selector":"h3.r a","extract":"text","type":"html","cache":true}
[noodle log #2]
Thu Jul 07 2016 00:09:44 GMT-0700 (PDT)
Memory: 19.00mb (19918848 bytes)
Cache: Page has been cached
[noodle log #3]
Thu Jul 07 2016 00:09:44 GMT-0700 (PDT)
Memory: 20.00mb (20967424 bytes)
Cache: Result has been cached
{ results: [ { results: [Object], created: 2016-07-07T07:09:44.286Z } ] }
【问题讨论】:
-
你试过没有jquery的例子吗?你可以在这里找到它github.com/dharmafly/noodle#noodle-as-a-node-module 或者 jquery 是你需要使用的东西吗?
-
感谢您的回复,我花了一些时间试图了解如何阅读它返回的结果,但我无法弄清楚这一点,是否有某种访问方式对象的内容?
标签: javascript jquery json ajax node.js