简体   繁体   English

如何用axios调用用C写的函数?

[英]How to call a function written in C with axios?

I've used a microcontroller to develop the embedded web server and I'm not sure if I can download an Apache or any other server into my controller.我已经使用微控制器开发了嵌入式 Web 服务器,但我不确定是否可以将 Apache 或任何其他服务器下载到我的控制器中。

However, I've successfully implemented an HTTP interface and have been hosting web pages and handling & parsing the POST request data/payload at the embedded web server side.但是,我已经成功地实现了一个 HTTP 接口,并且一直在托管网页并在嵌入式 Web 服务器端处理和解析 POST 请求数据/有效负载。

The issue is coming when the web page contains any form type data to be submitted.当网页包含要提交的任何表单类型数据时,问题就来了。

I receive the value(s) entered by the user on the web page but I'm not able to display the data properly on the web page sent by the embedded server.我收到了用户在网页上输入的值,但我无法在嵌入式服务器发送的网页上正确显示数据。

That's where there's a major issue in linking C (freeRTOS) code (server side) and JS (client side).这就是链接 C (freeRTOS) 代码(服务器端)和 JS(客户端)的主要问题所在。

How can a JS web client pull data from embedded web server (in freeRTOS) given that I have a successful HTTP connection established with the web page and I'm also able to host pages as mentioned above?假设我已经成功地与网页建立了 HTTP 连接并且我也能够如上所述托管页面,那么 JS Web 客户端如何从嵌入式 Web 服务器(在 freeRTOS 中)提取数据?

Currently I'm using axios but unable to figure out how to call a C function in the URL?目前我正在使用 axios 但无法弄清楚如何在 URL 中调用 C 函数? As it's not possible to code in C without a function.因为没有函数就不可能在 C 中编码。

axios({
method: 'post',
url: 'getStatus.c',
data: sampleData,
headers: {'Content-Type': 'multipart/form-data' }
})
.then(function (response) {
    console.log(response);
})

You cannot directly call a function in uncompiled C source file.您不能直接调用未编译的 C 源文件中的函数。

axios is a client side ( JS lib ) technology. axios 是一种客户端(JS 库)技术。 Any server side program that you want to interact with axios must implement some sort of HTTP interface.任何要与 axios 交互的服务器端程序都必须实现某种 HTTP 接口。

If you must use C to achieve something like that:如果你必须使用 C 来实现类似的东西:

  1. Implement a CGI interface in C在 C 中实现 CGI 接口

CGI program can as simple as something like this ( Handling POST request is bit more difficult ): CGI 程序可以像这样简单(处理 POST 请求有点困难):

#include <stdio.h>

int main()
{
  printf("Content-type: text/html\n\n");
  printf("<html>\n");
  printf("<body>\n");
  printf("<h1>Hello there!</h1>\n");
  printf("</body>\n");
  printf("</html>\n");
  return 0;
}

You can access POST request data the following way:您可以通过以下方式访问 POST 请求数据:

len_ = getenv("CONTENT_LENGTH");
len = strtol(len_, NULL, 10);
postdata = malloc(len + 1);
if (!postdata) { /* handle error or */ exit(EXIT_FAILURE); }
fgets(postdata, len + 1, stdin);
/* work with postdata */
free(postdata);

How to retrieve form "POST" data via cgi-bin program written in C 如何通过用 C 编写的 cgi-bin 程序检索表单“POST”数据

More on CGI C programs: http://jkorpela.fi/forms/cgic.html有关 CGI C 程序的更多信息:http: //jkorpela.fi/forms/cgic.html

Consider using libcgi http://libcgi.sourceforge.net for CGI C programs.考虑将 libcgi http://libcgi.sourceforge.net用于 CGI C 程序。

  1. Compile the CGI program.编译 CGI 程序。
  2. Use Apache2 or Nginx to serve the CGI "script" in this case the compiled binary.在这种情况下,使用 Apache2 或 Nginx 为 CGI“脚本”提供编译后的二进制文件。

If using C is not a priority:如果使用 C 不是优先事项:

I would recommend to use a high level language which is more suitable for web development.我建议使用更适合网络开发的高级语言。 Python / PHP / C# / Java / etc.. Python / PHP / C# / Java / 等..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM