Javascript AJAX POST查询因未知的\u协议而失败
我试图在mongodb中保存gjson功能,但它不起作用。。。 我正在使用我自己的server.jsJavascript AJAX POST查询因未知的\u协议而失败,javascript,ajax,node.js,Javascript,Ajax,Node.js,我试图在mongodb中保存gjson功能,但它不起作用。。。 我正在使用我自己的server.js "use strict"; // configurable portsetting var config = { httpPort: 8080, mongoPort: 27017 } var express = require('express'); var bodyParser = require('body-parser'); var mongoose = re
"use strict";
// configurable portsetting
var config = {
httpPort: 8080,
mongoPort: 27017
}
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var app = express();
app.use(bodyParser.urlencoded({extended: true})); // enable processing of the received post content
/* database schema for features */
var featureSchema = mongoose.Schema({
name: String,
dateInserted: Date,
data: {}
});
var Feature = mongoose.model('Feature', featureSchema);
/* database schema for routes */
var routeSchema = mongoose.Schema({
dateInserted: Date,
data: {}
});
var Route = mongoose.model('Route', routeSchema);
/* init database connection */
mongoose.connect('mongodb://localhost:' + config.mongoPort + '/ex06DB');
var database = mongoose.connection;
database.on('error', console.error.bind(console, 'ABORTING. database connection error:'));
// once db connection is open, start http server (Need to start db first, then server)
database.once('open', function (callback) {
console.log('connection to database established on port ' + config.mongoPort);
app.listen(config.httpPort, function(){
console.log('http server now running on port ' + config.httpPort);
});
});
/** http routing **/
// code which is executed on every request
app.use(function(req, res, next) {
console.log(req.method + ' ' + req.url + ' was requested by ' + req.connection.remoteAddress);
res.header('Access-Control-Allow-Origin', '*'); // allow CORS
next();
});
/* web app */
// deliver all contents of the folder '/webapp' under '/'
app.use(express.static(__dirname + '/webapp'));
// takes a json document via POST, which will be added to the database
// name is passed via URL
// url format: /addFeature?name=
app.post('/addFeature*', function(req, res) {
var title = req.url.substring(17, req.url.length);
var feature = new Feature({
name: title,
data: req.body
});
feature.save(function(error){
var message = error ? 'failed to save feature: ' + error
: 'feature saved: ' + feature.name;
console.log(message + ' from ' + req.connection.remoteAddress);
res.send(message);
});
});
这是我的“保存到数据库”功能:
/**
* saves the last drawn feature into the database
*/
function saveToDB() {
console.log("start: save to DB");
var name = prompt('Please give a name for the feature:');
var contentString = $('#drawnItemJSONText').val();
if ( name != undefined && contentString != '' ) {
var content = JSON.parse( contentString );
var url = 'localhost:8080' + '/addFeature?name=' + name;
// perform post ajax
$.ajax({
type: 'POST',
data: content,
url: url,
timeout: 1000,
success: function(data, textStatus ){
console.log("feature succesfully loaded into the database");
},
error: function(xhr, textStatus, errorThrown){
console.log("Sorry, could not load the feature into the database");
}
});
} else {
console.log("A Problem occured while adding to the database. No JSON-Object or name provided.");
}
};
回到我的问题上来。当进入saveToDB()
函数时,它会在我的控制台上返回错误消息,我不明白为什么。。。
我把POST功能搞砸了吗
我的控制台告诉我:
这些行是控制台.log
非常感谢您的帮助 您的url缺少http://
var url = 'http://localhost:8080' + '/addFeature?name=' + name;
您的url缺少http://
var url = 'http://localhost:8080' + '/addFeature?name=' + name;
错误消息是什么?
console.log(“对不起,无法将功能加载到数据库”)代码>。所以Post函数跳转到了代码的“错误部分”,我不明白为什么会这样。@ViktorG:你能发布准确的错误吗?或者在错误功能中,请您控制错误响应?@JayPatel当然!我包括了控制台的屏幕截图。我希望这就是你的意思。错误消息是什么?console.log(“对不起,无法将功能加载到数据库”)代码>。所以Post函数跳转到了代码的“错误部分”,我不明白为什么会这样。@ViktorG:你能发布准确的错误吗?或者在错误功能中,请您控制错误响应?@JayPatel当然!我包括了控制台的屏幕截图。我希望这就是你的意思。console.log(“对不起,无法将该功能加载到数据库”+textStatus+'-'+errorshown);请尝试此操作,并让我们知道错误代码,它会说:“抱歉,无法将功能加载到数据库错误-[异常…”URI方案对应于未知协议处理程序“nsresult:”0x804b0012(NS_error_unknown_protocol)”位置:“JS frame:::。send::line 4“data:no]”console.log(“抱歉,无法将功能加载到数据库”+textStatus+'-'+errorshown);请尝试此操作并让我们知道错误代码它说:“抱歉,无法将功能加载到数据库错误-[Exception…”URI方案对应于未知协议处理程序“nsresult:”0x804b0012(NS_error_unknown_protocol)”位置:“JS帧::发送::第4行“数据:否”