2015-05-21 2 views
2

Хотя у меня нет проблем с использованием Postman для отправки данных через NodeJS на MarkLogic 8, используя Angular, я не могу заставить его работать. req.body возвращает {}.MarkLogic 8 NodeJS Angular JSON transaction

Среди вещей, которые я пробовал добавляет это:

app.use(bodyParser.urlencoded({ 
    extended: true 
})); 

И:

app.use(function (req, res, next) { 

    // Website you wish to allow to connect 
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9040'); 

    // Request methods you wish to allow 
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 

    // Request headers you wish to allow 
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 

    // Set to true if you need the website to include cookies in the requests sent 
    // to the API (e.g. in case you use sessions) 
    res.setHeader('Access-Control-Allow-Credentials', true); 

    // Pass to next layer of middleware 
    next(); 
}); 

У меня есть следующая функция в моем контроллере:

function putFabriek($fabriek, $http) { 

    var mlUrl = 'http://localhost:3000/fabriek'; 
    console.log($fabriek); 

    $http({ 
     method: 'PUT', 
     url: mlUrl + '?id=' + $fabriek.id, 
     headers: {'Content-Type': 'application/json'}, 
     data: JSON.stringify($fabriek) 
    }).success(function(data, status) { 
     console.log($http.succes); 
    }).error(function(data, status) { 
     console.log($http.error); 
    });  
}; 

В NodeJS I имеют следующее:

// Fabrieken Puter 
app.put('/fabriek', function (req, res) { 
    var inhoud = req.body; 
    var fabId = req.query.id; 
    console.log(inhoud); 
    db.documents.write(
     { uri: ('/fabrieken/' + fabId + '.json'), 
      collections: ['examples', 'metadata-examples'], 
      contentType: 'application/json', 
      content: inhoud 
     }) 
     .result(null, function(error) { 
      console.log(JSON.stringify(error)); 
     }); 
    res.json({ message: 'Fabriek is updated!' });  
}); 

ответ

3

Вы отправляете JSON, поэтому вам нужно добавить соответствующие методы JSON body-parser.

app.use(bodyParser.json()); 
Смежные вопросы