Ajax 查询响应成功但引发错误
Ajax query response with success but it fires error
我是 mongodb 的新手,我正在使用 Express、Body-Parser 和 Mongoose 并且我有一个 mongodb 数据库(在线,称为 mlab),我从中提取数据。一切正常,我使用邮递员获取、Post 和删除。我正在尝试获取 JavaScript 中的数据,以便我可以在 html 中显示。我正在使用 Ajax,它可以工作并且 returns 成功,但是会触发 Ajax.
的失败函数
mongdb控制器
exports.photoalbum_all = function (req, res, next) {
PhotoAlbum.find(({}), function (err, photoalbum) {
if (err) return next(err);
res.send("../Views/images", {photo: photoalbum});
});};
我正在打的 Ajax 电话
function getPhoto() {
var photoAdd = 'http://localhost:1234/photoalbums/find';
$.ajax({
header: 'Access-Control-Allow-Origin: *',
dataType: "jsonp",
url: photoAdd,
contentType: 'application/json; charset=utf-8',
async: false,
crossDomain: true,
success: AjaxSucceeded,//callback,
error: AjaxFailed
});
}
function AjaxSucceeded(result) {
alert("hello");
alert(result);
}
function AjaxFailed(result) {
debugger;
alert("hello1");
alert(result.statusText);
}
getPhoto();
起初我收到有关 ajax 跨域请求被阻止 的错误,所以我不得不使用数据类型:'jsonp'。
就像我说的,当我检查 chrome 浏览器控制台的网络时,我获得了成功并且这个地址:
http://localhost:1234/photoalbums/find?callback=jQuery111202567313069615542_1545928466705&_=1545928466706
这 returns 这个 Json 值
[{"_id":"5c22a5ffcb29611f905f756b","title":"prayer","albums":[{"_id":"5c22a5ffcb29611f905f756c","u_name":"family01","u_title":"family_man"}],"createdAt":"2018-12-25T21:49:51.091Z","updatedAt":"2018-12-25T21:49:51.091Z","__v":0},{"_id":"5c22a66bd3527c39342fafe7","title":"prayer","albums":[{"_id":"5c22a66bd3527c39342fafe8","u_name":"family01","u_title":"family_man"}],"createdAt":"2018-12-25T21:51:39.274Z","updatedAt":"2018-12-25T21:51:39.274Z","__v":0}]
我已经用尽了自己的研究,但无济于事,不过我很怀疑,我从 mlab 得到的 Json 响应可能是问题所在,我的意思是格式。我愿意接受任何想法。提前感谢您的帮助。
除了JSONP,客户端无事可做。如果您想对服务器使用正确的 Ajax JSON 调用,则需要在本地主机上启用 CORS。
这里有一些关于如何操作的教程
您还可以 google 它:在本地主机上启用 CORS
希望对您有所帮助
我是 mongodb 的新手,我正在使用 Express、Body-Parser 和 Mongoose 并且我有一个 mongodb 数据库(在线,称为 mlab),我从中提取数据。一切正常,我使用邮递员获取、Post 和删除。我正在尝试获取 JavaScript 中的数据,以便我可以在 html 中显示。我正在使用 Ajax,它可以工作并且 returns 成功,但是会触发 Ajax.
的失败函数mongdb控制器
exports.photoalbum_all = function (req, res, next) {
PhotoAlbum.find(({}), function (err, photoalbum) {
if (err) return next(err);
res.send("../Views/images", {photo: photoalbum});
});};
我正在打的 Ajax 电话
function getPhoto() {
var photoAdd = 'http://localhost:1234/photoalbums/find';
$.ajax({
header: 'Access-Control-Allow-Origin: *',
dataType: "jsonp",
url: photoAdd,
contentType: 'application/json; charset=utf-8',
async: false,
crossDomain: true,
success: AjaxSucceeded,//callback,
error: AjaxFailed
});
}
function AjaxSucceeded(result) {
alert("hello");
alert(result);
}
function AjaxFailed(result) {
debugger;
alert("hello1");
alert(result.statusText);
}
getPhoto();
起初我收到有关 ajax 跨域请求被阻止 的错误,所以我不得不使用数据类型:'jsonp'。 就像我说的,当我检查 chrome 浏览器控制台的网络时,我获得了成功并且这个地址:
http://localhost:1234/photoalbums/find?callback=jQuery111202567313069615542_1545928466705&_=1545928466706
这 returns 这个 Json 值
[{"_id":"5c22a5ffcb29611f905f756b","title":"prayer","albums":[{"_id":"5c22a5ffcb29611f905f756c","u_name":"family01","u_title":"family_man"}],"createdAt":"2018-12-25T21:49:51.091Z","updatedAt":"2018-12-25T21:49:51.091Z","__v":0},{"_id":"5c22a66bd3527c39342fafe7","title":"prayer","albums":[{"_id":"5c22a66bd3527c39342fafe8","u_name":"family01","u_title":"family_man"}],"createdAt":"2018-12-25T21:51:39.274Z","updatedAt":"2018-12-25T21:51:39.274Z","__v":0}]
我已经用尽了自己的研究,但无济于事,不过我很怀疑,我从 mlab 得到的 Json 响应可能是问题所在,我的意思是格式。我愿意接受任何想法。提前感谢您的帮助。
除了JSONP,客户端无事可做。如果您想对服务器使用正确的 Ajax JSON 调用,则需要在本地主机上启用 CORS。
这里有一些关于如何操作的教程
您还可以 google 它:在本地主机上启用 CORS
希望对您有所帮助