views:

521

answers:

4

I am looking into AJAX for the first time and I would like to know if it's possible to make the requests from a server side CGI application written in C?

Will the C application just use printf for the data, similar to this .asp example?

+6  A: 

If I were you, I would stay away from C for server-side stuff. There are so many other languages that are better suited for this, but if you insist, you could use a library like cgic. Basically, you would just use the CGI handler from a server like Apache, but please, PLEASE use something other than C. It's very dangerous in the wrong hands, especially via CGI.

Use something like PHP or Perl to keep yourself sane. PHP is perfect for someone just starting out, and you won't have to futz around with compilation and making your CGI handler work/be secure.

Alex Fort
+1 for a link to a decent library surrounded by a healthy dose of warning.
Bill the Lizard
Thanks. I'm currently using the C CGIHTML library, similar to your suggestion. However, I am stuck using C at this time. Is it worth trying?
Tommy
Sure, it's worth trying if that's your only option. Just keep in mind that it's going to much harder to write, maintain, and debug than if you were using a language stack designed for this sort of thing.
Alex Fort
The vulnerabilities in C are well known. Php constantly has alerts for vulnerabilities. Even allowing for more users using php web frameworks, this does imply that you can't just assume that php will magic your security issues away.
Pete Kirkham
+1  A: 

ASP does some magic, such as outputting the appropriate response headers, but other than that it really is that simple. The server-side of AJAX is just responding to requests. Output the right data in the expected format and you're done. Stick with REST principles and this becomes easy.

dwc
A: 

You can do scripting using C.

Take a look at http://bellard.org/tcc/ - this is a small C99 compiler for Windows and UNIX. It has a unique feature: "-run" option. With this option, tinycc compiles the code into memory and executes it without creating an intermediate binary. Thus all you need to do is to create your CGI files like this:


#!path-to-tinycc-executable -run

// your C code goes below

....

However, I have to agree with previous commenters that C is not great for server side CGI.

valenok
A: 

I am looking into AJAX for the first time and I would like to know if it's possible to make the requests from a server side CGI application written in C?

I'll hop on the bandwagon, and say that it's usually easier to just use another language. However, I also understand that sometimes you have to use C - i.e., for embedded servers with limited resources. If that's the strongly suggest you use cgic, or heck, maybe even a framework like klone. I'm a bit biased towards the latter, though. It provides a nice interface to getting request objects, almost like the scripting variety of frameworks.