gpt4 book ai didi

javascript - 使用ajax post请求从 Node 服务器接收响应时出现问题

转载 作者:行者123 更新时间:2023-11-30 09:05:55 24 4
gpt4 key购买 nike

我用node js写了一个http服务器

var sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs");



http.createServer(function(request, res) {

var parsed_url = url.parse(request.url);
var uri = parsed_url.pathname;
if(uri === "/test"){

res.writeHead(200, {'Content-Type': 'text/javascript'});

request.addListener('data', function (chunk) {
var data = eval("(" + chunk + ")");
console.log(data[0].id);
})
request.addListener('end', function() {
console.log('end triggered');
res.write("Post data");
res.end();
});
}
}).listen(8080);

我正在尝试发回 ajax 请求的响应,但我无法收到任何响应。这是ajax请求的代码,

    var myhttp = new XMLHttpRequest();
var url = "http://localhost:8080/test";

var data = [{"a":"1"},{"b":"2"},{"c":"3"}];

var dataJson = JSON.stringify(data);
myhttp.open('POST', url, true);
myhttp.send(dataJson);
myhttp.onreadystatechange = function() {
if ((myhttp.readyState == 4) && (myhttp.status == 200)){
alert(myhttp.responseText);
}
else if ((myhttp.readyState == 4) && (myhttp.status != 200))
{
console.log("Error in Connection");
}

任何人都可以帮助我我做错了什么......

谢谢维奈

最佳答案

你的代码几乎是正确的,但在你的代码示例中你有

console.log(data[0].id)

data 对象有 no 属性 id 所以如果你只有

console.log(data[0])

你有这样的回应

{ a: '1' }

因此您可以通过以下方式访问属性a

console.log(data[0].a);

UPDATED 更新了一个完整的例子

还有一件事是您正在使用 eval 并且 node 带有 JSON.parse bundle 所以下面的代码片段是我如何让它工作的

文件:app.js

var sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs");



http.createServer(function(request, res) {
var parsed_url = url.parse(request.url);
var uri = parsed_url.pathname;

if(uri === "/test"){

res.writeHead(200, {'Content-Type': 'text/javascript'});
request.addListener('data', function (chunk) {
// removed this - eval("(" + chunk + ")");
var data = JSON.parse(chunk);
console.log(data[0].a);
})
request.addListener('end', function() {
console.log('end triggered');
res.write("Post data");
res.end();
});

} else if(uri === "/") {
fs.readFile("./index.html",function(err, data){
if(err) throw err;
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
});
}

}).listen(8080);

在同一目录上创建一个文件 index.html,内容如下:

<html>
<head>
<script type="text/javascript" charset="utf-8">
var myhttp = new XMLHttpRequest();
var url = "http://localhost:8080/test";

var data = [{"a":"1"},{"b":"2"},{"c":"3"}];
var dataJson = JSON.stringify(data);

myhttp.open('POST', url, true);
myhttp.send(dataJson);
myhttp.onreadystatechange = function() {
if ((myhttp.readyState == 4) && (myhttp.status == 200)){
alert(myhttp.responseText);
}
else if ((myhttp.readyState == 4) && (myhttp.status != 200))
{
console.log("Error in Connection");
}
}
</script>
</head>
<body>
</body>
</html>

这是您想要的完整工作示例。

关于同源策略问题,您遇到的主要是因为您不能通过 ajax 在 2 个不同的域之间发布数据,除非您对 iframe 使用一些技巧,但这是另一回事了。

另外,我认为在进入框架之前了解技术的主干对任何人来说都是有益的,这样对你来说是公平的。

祝你好运

关于javascript - 使用ajax post请求从 Node 服务器接收响应时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5026295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com