简体   繁体   English

无法在openshift上的nodejs和mongodb中发布

[英]Cannot POST in nodejs and mongodb on openshift

This is my ajax post request. 这是我的ajax发布请求。 I am using nodejs and mongodb.When I post a request I get an error stating CANNOT POST.currlocation in the POST request is the json object. 我正在使用nodejs和mongodb。当我发布请求时,我收到一条错误消息,指出无法发布。POST请求中的currlocation是json对象。 I have also tried to remove the content-type from POST request and tried to send the data as JSON.stringify(currlocation),it still doesnt work 我也尝试从POST请求中删除内容类型,并尝试将数据作为JSON.stringify(currlocation)发送,但仍然无法正常工作

     $.ajax({
        type: "POST",
        url: "http://myurl.rhcloud.com",
        contentType: "application/json",
        data: currLocation,
        dataType: "text",
        success: function( response ){
            console.log(response);                  

        },
        error: function( error ){
            console.log( "ERROR:", error );
        }
        });    

I am using nodejs and mongodb. 我正在使用nodejs和mongodb。 My configuration in server.js file is 我在server.js文件中的配置是

self.app.configure(function () {
            self.app.use(express.bodyParser());
            self.app.use(express.favicon());
            self.app.use(express.json());
            self.app.use(express.urlencoded());
            self.app.use(express.methodOverride());
            self.app.use(express.errorHandler({ dumpExceptions: true, showStack: true      }));


      });

My post request in server.js file is 我在server.js文件中的发布请求是

self.app.post('/', self.routes['post'] );
  self.routes['post'] = function(req, res){
        console.log("inside post");
               var mongojs = require('mongojs');
                var dbName = "/favloc";
                var connection_string = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +  process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" + process.env.OPENSHIFT_MONGODB_DB_HOST + dbName;
                console.log("conncetion string"+connection_string);
                var db = mongojs(connection_string, ['location']);
                res.setHeader('Access-Control-Allow-Origin','*');
                res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
                 res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');                
                 res.setHeader('Access-Control-Allow-Credentials', true);



               db.collection('location').insert({'city' : "sf",'id':'2'}, function(result){
                console.log("success");
                  res.send(req.body.self);
               //  res.end('success');
            });
       };

$.ajax() doesn't make real POST requests. $.ajax()不会发出真正的POST请求。

I think it actually sends a GET request with the Access-Control-Request-Method Header set to POST . 我认为它实际上发送的GET请求的Access-Control-Request-Method标头设置为POST

You can add support for this behavior by adapting your server-side code to match. 您可以通过调整服务器端代码使其匹配来添加对此行为的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM