如何创建Perl API以将数据传递回angularjs $ http请求?

时间:2022-08-22 17:42:22

I'm using angular 1.5.7 and perl 5.16.2. I don't have the ability to use any external libraries or switch tools - I'm stuck just using what's included with these two.

我使用的是角1.5.7和perl 5.16.2。我没有能力使用任何外部库或切换工具 - 我只是使用这两个包含的内容。

The angular controller is making calls to a perl file. I'm able to make POST requests successfully, and my GET requests are returning status 200 but aren't showing the data that I am expecting to see. Right now I'm just working with a simple example to try and narrow down where things are going wrong, and I think I just don't know the format to return values from the perl file.

角度控制器正在调用perl文件。我能够成功发出POST请求,我的GET请求返回状态200,但没有显示我期望看到的数据。现在我只是用一个简单的例子来尝试缩小出错的地方,我想我只是不知道从perl文件返回值的格式。

My controller GET method looks like so:

我的控制器GET方法如下所示:

    $http({
        method : "GET",
        url : "filename.pl",
        params:{}
    }).then(function successCallback(response) {
        console.log(response.records);
    }, function errorCallback(response) {
        console.log(response);
    });

and the perl code being called in filename.pl is:

并且在filename.pl中调用的perl代码是:

    my $string = q{{"records":{"2":{"City":"México D.F.","Country":"Mexico","Name":"Ana Trujillo Emparedados y helados"},"1":{"Name":"Alfreds Futterkiste","Country":"Germany","City":"Berlin"}}}};

     return $string;

The string is just a dummy variable to see if passing things back works - it won't be the final data. I have verified it is valid JSON as well. Here's what the response from the server looks like:

字符串只是一个虚拟变量,用于查看返回的内容是否有效 - 它不是最终数据。我已经验证它也是有效的JSON。以下是服务器的响应:

 {"data":"","status":200,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"glwizardutils.esp","params":{"FUNCTION":"initcheckboxes","CONTEXTID":"432"},"headers":{"Accept":"application/json, text/plain, */*"}},"statusText":"OK"}

The data field is totally blank, and the response field doesn't work at all. I can verify it's hitting the perl file - like I said, POST requests work fine and I included some printouts that verify it's in the correct method, but it's just not returning anything. What should I be doing here instead to get this to work?

数据字段完全空白,响应字段根本不起作用。我可以验证它是否击中了perl文件 - 就像我说的,POST请求工作正常,我包含了一些打印输出,用正确的方法验证它,但它只是没有返回任何东西。我应该在这做什么而不是让它发挥作用?

1 个解决方案

#1


1  

If filename.pl is invoked with HTTP, then it should be writing an HTTP response to standard output. At a minimum:

如果使用HTTP调用filename.pl,那么应该将HTTP响应写入标准输出。最低限度:

print "HTTP/1.1 200 OK\n";
print "Content-type: application/json\n";
print "\n";

my $string = q{{"records":{"2":{"City":"México ..."}}}};
print $string;

Several frameworks and modules exist in Perl and virtually every other language to handle the repetitive aspects of writing a proper response, which you will want to look into as your Perl server-side script gets more demanding.

Perl和几乎所有其他语言中都存在几个框架和模块来处理编写正确响应的重复方面,当您的Perl服务器端脚本要求更高时,您需要查看这些框架和模块。

#1


1  

If filename.pl is invoked with HTTP, then it should be writing an HTTP response to standard output. At a minimum:

如果使用HTTP调用filename.pl,那么应该将HTTP响应写入标准输出。最低限度:

print "HTTP/1.1 200 OK\n";
print "Content-type: application/json\n";
print "\n";

my $string = q{{"records":{"2":{"City":"México ..."}}}};
print $string;

Several frameworks and modules exist in Perl and virtually every other language to handle the repetitive aspects of writing a proper response, which you will want to look into as your Perl server-side script gets more demanding.

Perl和几乎所有其他语言中都存在几个框架和模块来处理编写正确响应的重复方面,当您的Perl服务器端脚本要求更高时,您需要查看这些框架和模块。