【发布时间】:2017-06-08 18:36:45
【问题描述】:
我正在尝试通过在一台服务器上运行的 Node.js 应用程序将 geoJSON 数据传递到在另一台服务器上运行的 Web 应用程序。我发现了很多其他与此相关的问题,但我尝试过的所有问题似乎都无法解决我的问题。
在 Chrome 上使用 RestEasy 扩展我可以毫无问题地使用我所有的 RESTapi 调用。获取、放置、删除和发布。将 url 粘贴到浏览器中也会返回带有 Chrome 控制台报告的 geoJSON 数据页面:
Resource interpreted as Document but transferred with MIME type application/json:
这是我第一次写这样的东西,我想将数据拉入客户端 web 应用程序以将 geoJSON 数据呈现到地图。
首先,我了解跨域设置需要我使用 JSONP,虽然我不熟悉我认为第一步是接收它,然后重新设计成 geojson 以便地图理解。 我将 CORS 安装到我的 Node.js 应用程序中,这似乎解决了我的跨域问题,但现在我收到了这个 jQuery 错误。
这是我的客户端 ajax 函数:
$(document).ready(function ()
{
console.log("Running AJAX");
$.ajax(
{
type: "GET",
contentType: "application/jsonp; charset=utf-8",
url: myUrl,
cache: false,
timeout: 5000,
dataType: "jsonp",
crossDomain: true,
data: "{}",
success: function (data)
{
// do my stuff
console.log("Reading Data");
console.log(data);
},
error: function (xhr, ajaxOptions, thrownError)
{
console.log("http Status Response: " + xhr.status);
console.log(thrownError);
}
});
});
这是我在服务器上的 Node.js 应用:
app.get('/api/objects', function(req, res)
{
Object.getObjects(function(err, objects)
{
if (err)
{
res.json(err);
}
res.jsonp(objects);
});
});
这是来自 Chrome 控制台的错误:
http Status Response: 200
Error: jQuery31108564601072970404_1485231436558 was not called
at Function.error (jquery.min.js:2)
at b.converters.script json (jquery.min.js:4)
at Nb (jquery.min.js:4)
at A (jquery.min.js:4)
at HTMLScriptElement.c (jquery.min.js:4)
at HTMLScriptElement.dispatch (jquery.min.js:3)
at HTMLScriptElement.q.handle (jquery.min.js:3)
【问题讨论】: