我目前正在设计自动停车系统的用户界面。我目前需要测试我的页面是否通过将其发送给监听器来从表单发送数据。我目前有这个代码,但我不确定为什么它不起作用。任何帮助将不胜感激。 这是我将代码发送到本地监听器的代码。
<script>
var INPARK = {cardID: $("#ticket_num").val(), lift: 1, floor: 1};
$.ajax({
type:"POST",
url: '192.168.150.148:5007',
contentType:"application/json",
data: JSON.stringify(INPARK)
});
</script>
这是听众代码。
var HOST = '192.168.150.148'; // This should be your IP of 192.168.150.XXX
var PORT = 5007;
var http = require('http');
http.createServer(function (req, res) {
// Only listen for POST requests
if (req.method === 'POST') {
var buffer = '';
req.on('data', function (chunk) {
buffer += chunk;
});
req.on('end', function () {
var path = req.url.substring(0, req.url.indexOf('/', 1)).toUpperCase();
var json;
try {
json = JSON.parse(buffer);
} catch (err) {
//
}
if (path === '/INPARK') {
// Handle INPARK request
console.log(json);
res.write('inpark results');
} else if (path === '/OUTPARK') {
// Handle OUTPARK request
console.log(json);
res.write('outpark results');
} else {
// Do nothing - Bad request
res.write('BAD REQUEST');
}
// Close the connection
res.end();
});
}
}).listen(PORT, HOST, function () {
console.log('Listening at %s:%s', HOST, PORT);
});
答案 0 :(得分:0)
您的ajax请求很可能是从端口80或443转到5007,这是一个跨域请求,因此它将失败,
如果您想解决此问题,请阅读CORS:
https://en.wikipedia.org/wiki/Cross-origin_resource_sharing,