따라서 전화 나 다른 데스크톱이 동일한 네트워크에서 실행 중인지 여부에 관계없이 클라이언트 측에서 일부 데이터를 입력 할 수있는 앱을 만들고 있습니다.요청 본문 Node.js 서버
내 요청에 응답하는 Node.js 서버가 있지만 서버 측에서 실제로 읽을 수는 없습니다.
이 서버에 대한 내 코드입니다
var http = require("http");
var parseString = require('xml2js').parseString;
var express = require('express')
var app = express();
var port = process.env.PORT || 8081;
var request = require('request');
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();
});
// start the server
app.listen(port);
console.log('Server started! At http://localhost:' + port);
// routes will go here
app.use('/static', express.static('.'));
var bodyParser = require('body-parser');
app.use('/browse', bodyParser.json()); // support json encoded bodies
app.use('/browse', bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.get('/', function(req, res, next) {
// Handle the get for this route
});
app.post('/', function(req, res, next) {
// Handle the post for this route
});
// POST http://localhost:8081/browse
app.get('/browse', function(req, res) {
var browseRequest = req.body.browseRequest;
console.dir("browseRequest: ");
console.dir(browseRequest);
res.send(JSON.stringify("response"));
});
는 (? 내가 여기에 여분의 물건을 할)
그리고 이것은 내 HTML 페이지
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
// Make the actual CORS request.
function makeCorsRequest() {
// This is a sample server that supports CORS.
var url = "http://192.168.0.100:8081/browse"
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
}
//xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
var req = { "browseRequest": {
"name": "Aaron",
"type": "Consumer",
"age": 21
}};
xhr.send(JSON.stringify(req));
}
을 위해 사용하고있는 부분이다 내 클라이언트에서 서버가 보낸 문자열을 얻을 수 있지만 req.body이 비어 있기 때문에 서버의 browserequest에 액세스 할 수 없습니다.
무엇이 여기에 있습니까? 감사합니다.
당신이 보낸 것을 서버에 알리지 못했습니다. (주석 처리 된 부분) –
CORS 오류가 발생하기 때문에 이전에 주석 처리했습니다. 주석을 달지 않은 채로도 몸이 비어 있습니다. 팁이 있습니까? – seugnimod