javascript - AJAX POST 查询失败并显示 UNKNOWN_PROTOCO

标签 javascript ajax node.js

我正在尝试将 gjson 功能保存在 mongodb 中,但它不起作用...... 我正在使用自己的 server.js

"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);                                  
    });
});

这是我的保存到 dB 功能:

/**
 * 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 功能?

我的控制台给了我这个:

enter image description here 这些行是 console.log

非常感谢您的帮助!

最佳答案

您的网址缺少 http://。

var url = 'http://localhost:8080' + '/addFeature?name=' + name;

关于javascript - AJAX POST 查询失败并显示 UNKNOWN_PROTOCO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163540/

相关文章:

javascript - 如何在 Javascript 中确定一年后

javascript - 从选中的复选框中获取值并使用 Javascript/jQuery 将它们放入数组中

javascript - 使用 Javascript 的语法高亮代码

c# - 在 ASP.Net 中使用 AJAX PageMethods 从数据库实现 jQuery AutoComplete TextBox

android - 错误 ENOENT : no such file or directory, 打开 'app/src/main/AndroidManifest.xml'

javascript - 使用 Autodesk 的 Forge OSS,我可以上传到存储桶,但下载的正文是空的

javascript - 如何在每个循环完成后发送响应

javascript - 如何克服 "potentially invalid usage of this"?

jquery - 如何通过 GitHub API 更新 Gist?

ruby-on-rails - 使用 ajax 提交表单后显示 flash 消息