javascript获取:can';无法从perl脚本获取响应后数据
我正在尝试从perl CGI脚本获取响应数据: 我有以下JavaScript代码:javascript获取:can';无法从perl脚本获取响应后数据,javascript,perl,fetch,Javascript,Perl,Fetch,我正在尝试从perl CGI脚本获取响应数据: 我有以下JavaScript代码: fetch('/test.pl', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ name: this.state.m
fetch('/test.pl', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: this.state.msgName,
email: this.state.msgEmail
})
})
.then((response) => {
console.log("been here");
console.log(response);
})
.then((responseData) => {
console.log("been here 2");
console.log(responseData);
})
.then((data) => {
console.log("been here 3");
console.log(data);
})
.catch((error) => {
console.log("Failed!", error)
});
fetch('/test.pl', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: this.state.msgName,
email: this.state.msgEmail
})
})
.then((response) => {
response.json().then((data) => {
console.log(data);
});
return(response);
}
})
这个perl脚本:
#/usr/bin/env perl
use strict;
use CGI;
use JSON;
my $q = new CGI;
my $json = new JSON;
if ($q->param)
{
print $q->header();
my $hash = $json->decode($q->param('POSTDATA'));
print $q->header('application/json');
my $msg = [ sprintf "data from %s received ok", $hash->{name} ];
print $json->encode($msg);
}
但我只得到了这个(我使用的是Chrome的Web开发工具),没有数据:
been here
Response {type: "basic", url: "http://localhost/test.pl", status: 200, ok: true, statusText: "OK"…}
been here 2
undefined
been here 3
undefined
我确信perl脚本工作正常,下面是从命令行测试它的输出:
# curl -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"name":"uvw","email":"xyz"}' http://localhost/test.pl
Content-Type: application/json; charset=ISO-8859-1
["data from uvw received ok"]%
有人知道如何将数据响应返回到js脚本吗?提前感谢您的帮助 您没有在回调链中传递数据。如果您希望在下面的
then()
处理程序中返回某个内容,则必须从then()
处理程序返回该内容
由于第一个then()
处理程序没有return
语句,因此它隐式返回undefined
,这就是您在下一个处理程序中看到的
如果您不做任何额外的异步工作,通常不需要多次调用then()
。我终于解决了这个问题,我在这里发布我的解决方案,因为它可能与其他有类似问题的人相关
问题有两方面:
我从Perl脚本中打印了一个额外的HTML头,这使得JSON数据出错
我没有在premise中使用response.json()来获取json数据
以下是固定代码:
JavaScript代码:
fetch('/test.pl', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: this.state.msgName,
email: this.state.msgEmail
})
})
.then((response) => {
console.log("been here");
console.log(response);
})
.then((responseData) => {
console.log("been here 2");
console.log(responseData);
})
.then((data) => {
console.log("been here 3");
console.log(data);
})
.catch((error) => {
console.log("Failed!", error)
});
fetch('/test.pl', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: this.state.msgName,
email: this.state.msgEmail
})
})
.then((response) => {
response.json().then((data) => {
console.log(data);
});
return(response);
}
})
Perl代码:
#/usr/bin/env perl
use strict;
use CGI;
use JSON;
my $q = new CGI;
my $json = new JSON;
if ($q->param)
{
print $q->header();
my $hash = $json->decode($q->param('POSTDATA'));
# NEXT LINE REMOVED, IT WAS WRONG!!
#print $q->header('application/json');
my $msg = [ sprintf "data from %s received ok", $hash->{name} ];
print $json->encode($msg);
}
好的,如果我按照你说的做,我不再有“未定义”了,但是我仍然没有从perl脚本中获得响应数据,CGI脚本是否工作?您确实有一个响应
对象,根据您自己的日志,我已经使用curl通过命令行进行了测试,并正确返回了数据(请参阅我在文章末尾写的内容)