` $("#but1").click(function ()
{
var data = { "temp": text };
$.ajax({
type: 'POST',
data: JSON.stringify(data),
datatype: 'jsonp',
contentType: 'application/json',
url:"https://thermostatapp.azurewebsites.net/api/updatetemp",
crossDomain: true,
success: function (data) {
alert('success');
},
});
} `
developer tool image showing eror
数据被发布到服务器,但是从回调函数我在2分钟后收到内部服务器错误。 服务器端代码是
'use strict';
var express = require('express');
var app = express();
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var cors = require("cors");
app.use(cors({
origin: '*'
}));
var http = require('http');
var port = process.env.PORT || 1337;
http.createServer(app).listen(port);
app.post('/api/updatetemp', function (req, res) {
var temper = req.body.temp;
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("unity");
var myquery = { first_name: "sid" };
var newvalue = {
$set: { temp: temper }
}
dbo.collection("user_list").updateOne(myquery, newvalue, function (err, result) {
if (err) throw err;
else
console.log("update!1!")
db.close();
});
});
我怎么找到导致此问题的原因?由于我使用的是Express Framework,发送带有ajax调用的csrf令牌会解决该错误吗?