无法在jquery ajax中发布

时间:2014-04-21 21:05:21

标签: jquery ajax node.js

这是我的ajax帖子请求。当我向nodejs发布请求时,我收到一条错误,指出CAN NOT POST。我的服务器端脚本在nodejs express中。

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

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

更新: 这是我的server.js文件

我在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');
            });
       };

0 个答案:

没有答案