Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


help on 1st ajax try

help on 1st ajax try

help on 1st ajax try

I visited the ajax tutorial in w3cschools yesterday, the following html is mostly copied directly from that tutorial:
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML= "";
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange= function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML= xmlhttp.responseText;
xmlhttp.open("GET", "....cgi-bin/ajax_tst.cgi?str=" + str, true);

<p><b>Start typing a letter in the input field below:</b></p>
enter a lettr: <input type="text" onkeyup="showHint(this.value)">
<p>Suggestions: <span id="txtHint"></span></p>

The following perl is my 1st try to run on my server
use strict;
use CGI;
our $qq= new CGI;
print $qq->header("text/xml"); # I have tried to not print this, but failed also

my $str=$qq->param("str");

#print "<?xml version='1.0'?><response>you entered $str</response>"; # I tried this one but did not work
#print "you entered $str"; # tried this one also did not work
print "<html><body>you entered $str</body></html>"; # neither this one works

open(OUT,">/var/www/htm/upload/junk.txt"); # for debugging
print OUT "seeing $str\n"; # I saw the correct result on my server, proving that ajax has transferred the str from browser to server correctly

But this perl did not correctly transfer the result back to the browser,
What is the problem?

Thank you in advnace for helping!

RE: help on 1st ajax try


First of all, please enclose the code you post here between [code] and [/code] TGML tags.

Then please explain what you mean by "did not correctly transfer the result back". In case your web server's error log mentions something, please post that too. Otherwise would be hard to help, as the code you posted works for me.

A quick checklist in meantime ( please forgive the stupid ones, no idea how familiar you are with the web things ) :
  • You are accessing the CGI script through HTTP protocol.
  • Perl interpreter or web server module is installed and the web server is configured to use it.
  • The CGI script file has execute permission for the user or group that runs the web server.
  • Perl CGI module is installed.
And a couple of things that I would do differently anyway, though should make no difference in this phase :
  • The CGI response contains HTML, so better say so : print $qq->header("text/html");. ( Yes, this line is needed. )
  • The CGI response will be used as part of a HTML document, so send only a HTML fragment, not an entire document : print "you entered $str";, maybe print "<p>you entered <b>$str</b></p>";. ( After the response gets inserted into original document, that should be still valid HTML. )


RE: help on 1st ajax try

Thank you very much for your meticulous explanation.
Now the code on the server is modified as you instructed:


use strict;
use CGI;
our $qq= new CGI;
print $qq->header("text/html");

my $str=$qq->param("str");

print "your entered:$str";

print OUT "seeing $str\n";
(but it still does not work)

Answering your question about CGI: yes I have hundreds of perl CGI programs running for years, except no Ajax yet.
What I meant "did not correctly transfer the result back" is that I did not see what I typed echoing on the invoking browser page.
If ajax runs correctly, after I entered "j", for example, on the web page, a few seconds later I should see the web page showing
"Suggestions:you entered j", buy I didn't see it.

On the server side, I did see "seeing j" in the file junk.txt(that means ajax did transferred "j" from browser to server correctly). Also I checked the server error log there is nothing about this program.

RE: help on 1st ajax try


Quote (gj165)

Answering your question about CGI: yes I have hundreds of perl CGI programs running for years, except no Ajax yet.
Now that you mentioned it, could it be CORS ( Cross-Origin Resource Sharing ) issue ? I mean, are the HTML page that performs the AJAX request and that "....cgi-bin/ajax_tst.cgi" on the same protocol + domain + port ?

If that is the problem, in the browser's console should appear a message like this :

Quote (console)

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ..../cgi-bin/ajax_tst.cgi?str=j. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
In such case the CGI script runs without problem, writes to junk.txt, outputs response, but the browser will not allow the JavaScript code to find out anything about it.

And forgot earlier : terminating the Perl file with 1; ( in other words returning a value that evaluates to true ) is only needed for packages.


RE: help on 1st ajax try

yes you are right! I checked the console, it says:

XMLHttpRequest cannot load http:..../ajax_tst.cgi?str=a. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

How to set the requesting html or the browser to allow it?
(where to add: Access-Control-Allow-Origin: http:...my server.com?)

Thanks a million!

RE: help on 1st ajax try


Just change the CGI::header() call to return the necessary HTTP response header too :

CODE --> ( fragment )

print $qq->header(
  -type => 'text/html',
  -access_control_allow_origin => '*',

Note that this way any web site will be able to send AJAX request to your server. If you not want to allow that, replace the * with the protocol and domain ( no path ) where the HTML document with the form is.


RE: help on 1st ajax try

It works now!

Thanks a million, sincerely, thanks a million!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close