我在nodejs上创建了一个简约API,它以JSON格式返回数据。jQuery.ajax后出现意外的令牌冒号JSON#
但每次我试图使一个AJAX#get调用,并通过我的API为URL的时候,我会得到一个错误,从Chrome的情况来看,我得到一个错误"Unexpected token :"
;
在这里服务器代码的NodeJS + 表达:
var
http = require('http'),
express = require('express'),
app = express(),
server = http.createServer(app);
app.get('/', function(req, res) {
console.log('req received');
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({
Name : "Tom",
Description : "Hello it's me!"
}));
});
server.listen(3000, function() {
console.log('Listening on 3000');
});
的JSON从"/"
返回是:{"Name":"Tom","Description":"Hello it's me!"}
。
这里是从客户端的js我的电话:
$.ajax({
url: findUrl,
type: 'get',
dataType: 'jsonp',
success: function (data) {
self.name(data.Name);
self.description(data.Description);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
当绘制我得到的错误:"jQuery111108398571682628244_1403193212453 was not called"
有人能帮助我吗?
我知道这个问题已被问到,但我还没有设法找到解决方案,修复我的程序。
检查'的dataType ' - [$ .ajax()](http://api.jquery.com/jquery.ajax/) – Andreas
@Andreas:我想你指的是“jsonp”,我将它改为“json”。现在我得到“XMLHttpRequest无法加载http:// localhost:3000 /。没有'Access-Control-Allow-Origin'标题出现在请求的资源上,因此Origin'null'不允许访问。我怎样才能解决这个问题 ? –