【问题标题】:success method wont call - nodejs using express成功方法不会调用-使用express的nodejs
【发布时间】:2014-04-28 03:27:33
【问题描述】:

所以我的 nodeJs 服务器代码中有这个方法,它需要向 mongoDB 插入一些东西,问题是即使它插入了什么(我在它之后的数据库中看到)它也不会进入成功调用。

我只想在我的客户中发出警报说“完成”

我正在通过 express 使用 nodejs。

这是我的服务器端:

exports.savePre = function(db) {
    return function(req, res) {

        // Get our form values. These rely on the "name" attributes
        var json = req.body.jsonToSave;
        var date = req.body.date;
        var name = req.body.name;
        var ArrayOfSlice = req.body.ArrayOfSlice;


        // Set our collection
        var collection = db.get('PresentationCollection');

        // Submit to the DB
        collection.insert({
            "JsonToSave": json,
            "Date": date,
            "Name": name,
            "ArrayOfPoints": ArrayOfSlice
        }, function (err, inserted)
        {
            res.writeHead(200, { 'content-type': 'text/plain' });
            res.end();

            if (err)
            {
                // If it failed, return error
                res.send("There was a problem adding the information to the database.");
            }
            else
            {
                res.send("bla");

                //res.writeHead(200, { 'content-type': 'text/plain' });
                //res.end();
                //res.setHeader("Access-Control-Allow-Origin", "*");
                // If it worked, set the header so the address bar doesn't still say /test
                //res.end();
                // // And forward to success page
                // res.redirect("userlist");
             }
        });

    }
}

这是我在客户中的电话:

  function savePre()
        {
            var exporter = new THREE.SceneExporter();
            var sceneJInson = JSON.stringify(exporter.parse(scene));

            var CameraPosition = camera.position;
            //var CameraLookAt = lookAtPosition;

            // Array Of Slice, each row is a slice - here we will add all the slices :)
            var arrayOfSlice = [{ cameraPosition: CameraPosition }];
            //                    { cameraPosition: CameraPosition, CameraLookAt: CameraLookAt },
            //                    { cameraPosition: CameraPosition, CameraLookAt: CameraLookAt }];

            // TODO : give the name where the user chose, this is only test, date is also a test
            var parameters = { name: "eranNew", date: new Date(), jsonToSave: sceneJInson, ArrayOfSlice: JSON.stringify(arrayOfSlice) };

            $.ajax({
                url: '/savePre',
                type: 'POST',
                data: parameters,
                success: function (result) {
                    alert("done");
                },
                error: function (result) {
                    alert("error");
                },
                dataType: 'json'
            });
        }

任何帮助都会得到回报。

【问题讨论】:

    标签: jquery ajax node.js mongodb


    【解决方案1】:

    那是因为你写了好几次标题。

      collection.insert({
            "JsonToSave": json,
            "Date": date,
            "Name": name,
            "ArrayOfPoints": ArrayOfSlice
        }, function (err, inserted)
        {
    
            if (err)
            {
                res.send(500, "There was a problem adding the information to the database.");
            }
            else
            {
                res.send(200, "bla");
    
             }
        });
    

    【讨论】:

    【解决方案2】:

    我可以通过使用这个来解决它:

     if (err)
                {
                    res.send(500, "There was a problem adding the information to the database.");
                }
                else
                {
                    res.statusCode = 200;
    
                    res.json("OK");
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      相关资源
      最近更新 更多