使用UNKNOWN_PROTOCO,AJAX POST查询失败

时间:2016-08-26 09:57:14

标签: 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   = 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

非常感谢帮助!

1 个答案:

答案 0 :(得分:2)

您的网址缺少http://。

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