繁体   English   中英

$ .Deferred()和$ .ajax()在Node.JS中不起作用

[英]$.Deferred() and $.ajax() not working in Node.JS

我有以下实现。

import _ from 'lodash';
import test from 'tape';
import 'jsdom-global/register';
let jQuery = require('jquery')(window);

let $ = global.jQuery = jQuery;

test('check if Deferred work?', (t) => {
  let dfd = $.Deferred();
  let ajax = $.ajax();

  console.log($.extend({}, {a: 1, b:2}), 'dfd', dfd, 'ajax', ajax);
  t.equal(true, true, 'falsey condition');
  t.end();
});

$ .extend有效,但dfdajax无效。 知道如何使它起作用吗?

错误读取:TypeError:$ .Deferred不是函数TypeError:$ .ajax不是函数

谢谢

编辑 :(可能的解决方案,但我非常讨厌jsdom库更改,因此,IDK如果以下内容将像弃用的jsdom.env()那样破坏一天的jsdom.env()

const { JSDOM } = require('jsdom');
const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;
import * as jquery from "jquery";
import test from 'tape';
import _ from 'lodash';

test("jquery tests", (t) => {
  t.plan(2);
  const $ = require("jquery")(window);
  $.ajax({url: "http://freegeoip.net/json/"}).done(data => {
    console.log("data is " + JSON.stringify(data));
    t.equal(true, true);
    t.ok(true);
    t.end();
  });
});

输出

$ npx babel-tape-runner ./src/jquery-test.spec.es6.js
TAP version 13
# jquery tests
data is {"ip":"188.90.2xx.4","country_code":"US","country_name":"United States","region_code":"CA","region_name":"California","city":"Orange","zip_code":"92866","time_zone":"America/Los_Angeles","latitude":33.7846,"longitude":-117.8433,"metro_code":803}[object Object]
ok 1 should be equal
ok 2 should be truthy

jQuery提供了两种扩展方法。 一种是静态方法,另一种是实例方法。 您使用require('jquery')(window).extend实际上使用实例方法。 但是jQuery.Deferred是静态方法,不能由jQuery实例调用。

jQuery.extend:将两个或更多对象的内容合并到第一个对象中。 jQuery.fn.extend:将对象的内容合并到jQuery原型上以提供新的jQuery实例方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM