我试图让 dojo 1.8 的 dojo/request 在 OS X 10.8.2 Mac 上的 node.js v0.8.17 下工作。
我尝试了来自dojotoolkit.org的两个示例,这两个示例都失败并出现相同的错误:
module.js:236
var start = request.substring(0, 2);
^
TypeError: Object require,dojo/request has no method 'substring'
at Function.Module._resolveLookupPaths (module.js:236:23)
at Function.Module._resolveFilename (module.js:328:31)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/Users/chris/src/bsn/INF/spikes/dojo_1-8/example/example.js:12:1)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
这是基于dojo 教程的第一个示例:
// dojo-release-1.8.3-src is installed in the same directory as this script
dojoConfig = {
baseUrl: ".",
packages:[{name: 'dojo', location: 'dojo'}],
deps:['dojo/request', 'dojo/date']
};
require('./dojo/dojo.js');
console.log("I can call dojo/date functions no problem:");
console.log("e.g. Time Zone is:" + dojo.date.getTimezoneName(new Date()));
console.log("But when I try to use dojo/request it fails:");
// Following taken from:
// http://dojotoolkit.org/documentation/tutorials/1.8/ajax/
require(["dojo/request"], function(request){
request("helloworld.txt").then(
function(text){
console.log("The file's contents is: " + text);
},
function(error){
console.log("An error occurred: " + error);
}
);
});
这是从dojo 参考指南中摘录的第二个:
// dojo-release-1.8.3-src is installed in the same directory as this script
dojoConfig = {
baseUrl: ".",
packages:[{name: 'dojo', location: 'dojo'}],
deps:['dojo/request', 'dojo/date']
};
require('./dojo/dojo.js');
// Following is taken from:
// https://dojotoolkit.org/reference-guide/1.8/dojo/request/node.html#id5
require(['require', 'dojo/request'], function(require, request){
var http = require.nodeRequire('http'),
timeout;
var server = http.createServer(function(request, response){
var body = '{ "foo": "bar" }';
response.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'application/json'
});
response.write(body);
response.end();
});
server.on('close', function(){
if(timeout){ clearTimeout(timeout); }
});
server.on('listening', function(){
request.get('http://localhost:8124', {
handleAs: 'json',
headers: { 'Range': '1-2' },
timeout: 1000
}).then(function(data){
console.log(data);
server.close();
}, function(err){
console.log(err);
server.close();
});
});
server.listen(8124);
});
我不明白为什么在第一个示例中对 dojo.date 的调用可以正常工作,但 dojo.require 却不行。
有任何想法吗?
谢谢
克里斯