gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:14:29 25 4
gpt4 key购买 nike

我正在尝试将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com