javascript - 如何在 Node.js 中调用 $.ajax
问题描述
如何在 Node.js 中进行 jQuery Ajax 调用?app.js 输出以下错误:
$.ajax 不是函数
应用程序.js
var $ = require('jQuery');
$.ajax({
url: "https://api.example.com/resource/example.json",
type: "GET",
data: {
"$limit" : 5000,
"$$app_token" : "MY_TOKEN"
}
}).done(function(data) {
// Logic
});
解决方案
使用 request 或 axios 甚至节点中的核心 http 模块而不是 jQuery。
使用请求的示例:
var request = require('request');
request({ url: 'http://api.example.com/resource/example.json’, qs: {
"$limit" : 5000,
"$$app_token" : "MY_TOKEN"
} }, function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode);
// Print the response status code if a response was received
console.log('body:', body); // Print the HTTP body
});
推荐阅读
- angular - Angular 2 CLi ng new 不创建项目
- sql-server - 创建了一个 SQL 代理作业来运行一个使用 FTP 连接的包
- css - 使用 SVG 组件的 css 样式在点击时使可见性“粘滞”
- stored-procedures - 无法使用数字分区键查询 Cosmos DB
- javascript - 如何在节点上下文中抑制 window.function 调用,例如未实现:window.scrollTo
- javascript - 如何让 firebase 将最后 5 个搜索结果显示到 HTML div?
- google-apps-script - 我想要一个脚本按钮,将 A 列的值减去 B 列的值
- laravel - 验证 Laravel 中的唯一名称和名字
- dart - Dart Aqueduct 服务器基本授权
- python - “如何为字典创建计数器”