无法在openshift上的nodejs和mongodb中发布 [英] Cannot POST in nodejs and mongodb on openshift

查看:89
本文介绍了无法在openshift上的nodejs和mongodb中发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的ajax发布请求.我正在使用nodejs和mongodb.当我发布请求时,我收到一条错误消息,指出无法发布.POST请求中的currlocation是json对象.我也尝试从POST请求中删除内容类型,并尝试将数据作为JSON.stringify(currlocation)发送,但仍然无法正常工作

This is my ajax post request. 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. 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

     $.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 );
        }
        });    

我正在使用nodejs和mongodb.我在server.js文件中的配置是

I am using nodejs and mongodb. My configuration in server.js file is

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


      });

我在server.js文件中的发帖请求是

My post request in server.js file is

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()不会发出真正的POST请求.

$.ajax() doesn't make real POST requests.

我认为它实际上发送的GET请求的Access-Control-Request-Method标头设置为POST.

I think it actually sends a GET request with the Access-Control-Request-Method Header set to POST.

您可以通过调整服务器端代码以使其匹配来增加对此行为的支持.

You can add support for this behavior by adapting your server-side code to match.

这篇关于无法在openshift上的nodejs和mongodb中发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆