我正在尝试使用Restify将字符串发布到Node服务器。我的帖子'请求看起来像这样:
// AJAX image save
function saveImage() {
var xmlhttp = new XMLHttpRequest();
var url = 'http://127.0.0.1:7070/server.js';
var params = document.querySelector('img').src;
console.log(params);
xmlhttp.open("POST", url, true);
//Send the proper header infomation along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var response = xmlhttp.responseText;
// Callback
if(response == 'false')
{
return(false);
}
else
{
return(true);
}
}
}
xmlhttp.send(params);
}
然后在服务器上,我有以下设置:
// Setup server.
var restify = require('restify'),
imageSave = require('save')('image');
var server = restify.createServer({ name: 'imageCapture'});
server.use(restify.acceptParser(server.acceptable));
server.listen(7070, '127.0.0.1', function() {
console.log('%s listening at %s', server.name, server.url);
});
server
.use(restify.fullResponse())
.use(restify.bodyParser())
// Saving bit...
server.post('/images', function (req, res, next) {
console.log(req, res);
res.setHeader('Access-Control-Allow-Origin','*');
imageSave.create({ image: req.params.name }, function (error, image) {
res.send(201, image)
})
})
我是否在服务器端Javascript中设置了标头?那是接受发布请求吗?