You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node环境下TypeError: $.ajax is not a function问题解决求助

解决Node.js环境中$.ajax is not a function的问题

Hey there, let's break down why you're seeing this error and walk through the fixes:

问题根源

When you require('jquery') in Node.js, you're not getting the same jQuery object you know from browsers. jQuery was built for browser environments, where it has access to the DOM and XMLHttpRequest (which powers $.ajax). In Node.js, those browser APIs don't exist by default—so the jQuery package from npm doesn't include the ajax method out of the box.

The <script> tag solution you found works for browsers, but that's not how dependencies work in Node.js. Let's cover two solid fixes:


方案1:配置jQuery在Node.js中模拟浏览器环境

We can use jsdom to create a fake browser window environment, which lets jQuery access the APIs it needs for $.ajax.

  1. First, install the required packages:
npm install jquery jsdom
  1. Update your code to set up jQuery with jsdom:
const { JSDOM } = require('jsdom');
const { window } = new JSDOM();
const $ = require('jquery')(window);

var url_path = "http://192.168.1.232:9000/get";
$.ajax({
    type: "GET",
    url: `${url_path}?callback=?`,
    dataType: 'jsonp',
    async: true,
    error: function () {
        console.log("Error has occurred..."); // Node.js doesn't have `alert()`—use console.log instead
    },
    success: function(data) {
        console.log("Sent to CLI successfully!!!");
    }
});

Note: I replaced alert() with console.log() because Node.js doesn't have a browser alert system.


方案2:使用Node.js原生工具或专门的HTTP请求库(更推荐)

jQuery's ajax isn't the best fit for Node.js. There are better tools built specifically for server-side HTTP requests. Here are two popular options:

用jsonp库处理JSONP请求

Since your request uses JSONP, the jsonp npm package is a lightweight fit:

  1. Install it:
npm install jsonp
  1. Rewrite your code:
const jsonp = require('jsonp');

const url = "http://192.168.1.232:9000/get?callback=?";
jsonp(url, null, function (err, data) {
    if (err) {
        console.log("Error has occurred...");
        console.error(err);
    } else {
        console.log("Sent to CLI successfully!!!");
        console.log(data);
    }
});

用axios处理常规HTTP请求(如果JSONP isn't strictly needed)

For standard GET/POST requests, axios is a robust, promise-based library:

  1. Install axios:
npm install axios
  1. Example code:
const axios = require('axios');

const url_path = "http://192.168.1.232:9000/get";
axios.get(url_path)
.then(response => {
    console.log("Sent to CLI successfully!!!");
    console.log(response.data);
})
.catch(error => {
    console.log("Error has occurred...");
    console.error(error);
});

If you still need JSONP with axios, you can use the axios-jsonp-adapter package to add that support.


内容的提问来源于stack exchange,提问作者jny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:09:24