我的路由器代码
var express = require('express');
var router = express.Router();
function sendSolve(test) {
// Some code for calculations
}
/* POST listing. */
router.post('/', function (req, res, next) {
sendSolve(req.body.data);
next();
}, function (req,res) {
res.json({"message": "ok"});
});
module.exports = router;
客户代码:
$('.button').on('click', function () {
var $textArea = $('#textArea');
var newData={data: $textArea.val()};
$.post('/', newData, function (res) {
console.log(res);
$textArea.val('');
});
});
然后我首先$.post
everythng没问题,但是在第二篇文章中我得到了错误
500 (Internal Server Error)k.cors.a.crossDomain.send @ jquery-2.1.4.min.js:4n.extend.ajax @ jquery-2.1.4.min.js:4n.(anonymous function) @ jquery-2.1.4.min.js:4(anonymous function) @ main.js:26n.event.dispatch @ jquery-2.1.4.min.js:3r.handle @ jquery-2.1.4.min.js:3
答案 0 :(得分:0)
500 (Internal Server Error)k.cors.a.crossDomain.send
对我来说似乎是一个CORS问题。很常见的POST。解决此问题的一种方法是通过中间件启用CORS。示例如下:
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
确保将其置于POST路线之上。希望有所帮助。