내 코드는 내가 (이미 패키지를 설치 npm install jquery
를 실행 한) jQuery의 AJAX 기능을 연습하려면, 다음과 같습니다
var $ = require('jquery');
var remoteValue = false;
var doSomethingWithRemoteValue = function() {
console.log(remoteValue);
}
var promise = $.ajax({
url: 'https://google.com'
});
//outputs "true"
promise.always(function() {
remoteValue = true;
doSomethingWithRemoteValue();
});
//outputs "foobar"
promise.always(function() {
remoteValue = 'foobar';
doSomethingWithRemoteValue();
});
을하지만 NodeJS 컴파일러는 jQuery를 모듈에는 ajax
방법이 없다고 불평한다.
bash-3.2$ node test.js
/Users/hanfeisun/Downloads/node/test.js:14
var promise = $.ajax({
^
TypeError: Object function (w) {
if (!w.document) {
throw new Error("jQuery requires a window with a document");
}
return factory(w);
} has no method 'ajax'
at Object.<anonymous> (/Users/hanfeisun/Downloads/node/test.js:14:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
브라우저 JS 컴파일러와 NodeJS에 대한 jQuery의 javascript 코드는 같지 않아야합니까?
그렇지 않다면 주된 이유에 대해 누구나 생각할 수 있습니까? 감사!
'다른 언어'가 아닙니다. 자바 스크립트입니다. 대부분의 사람들이 익숙한 DOM API와 네트워킹 API는 브라우저에만 존재하며 노드의 API는 노드에만 존재합니다. jQuery는 DOM API와 XMLHttpRequest를 사용할 수있는 곳이면 어디에서나 실행할 수 있습니다. – loganfsmyth