Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI to send non 200 status codes

Status
Not open for further replies.

jez

Programmer
Apr 24, 2001
370
VN
Hi All,

I have a script which is fairly short, it simply saves the submission to a database and then returns.

Near the top i had
Code:
print "Content-type: text/html\n\n";

Everything worked.
At the bottom i was returning a string, either telling me it was success or failure.

Now i am calling this script with AJAX and no matter what happens that sees a successful status and thinks there was no problem.

I have now removed the header for the content type instead in my output at the bottom putting the following code;-

Code:
 if($sth->errstr) {
                 
                 print $query->header('text/html','204 No response');
                 
            }else{
                print "Content-type: text/html\n\n";
                print "Record stored for ".$session_id ." on local db"; 
            }

All i am getting now is premature end of script headers.

I have gone through and there are no calls to print anything before the end of the script. There is no output from the script atall.

If someone could give a little bit of background to how to send non-ok status codes that would be great, i am finding the documentation and web searches only say to add a "content-type" at the top of the script...

thanks.

jez
 
The doc for CGI.pm has an example
Code:
print header('text/html','204 No response');
which is slightly different from yours. I'm assuming their one works, but this may be a wrong assumption...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks for the suggestion, unfortunately it is not stopping the premature end of script errors.

I think that there is something other than a print statement producing output before i send a content type header.
I have searched for everything i can but cant see why i cannot conditionally decide on the response type based on the script's execution.

Thanks anyway.

Jez
 
All i am getting now is premature end of script headers.
"Premature end of script headers" generally has nothing to do with the http headers your script might (or might not) be printing. It's just one of the things you can get when a script fails. Try adding this to the top of your script:
Code:
   use CGI::Carp qw(fatalsToBrowser);

-- Chris Hunt
Webmaster & Tragedian
Extra Connections Ltd
 
Yes i was using that, and it gave me a "premature end of script headers".

Sorry if i am sounding less than useful, but i have tried the basic errors trapping stuff.
The problem i have is that i only get the "premature..." when i do not put print "Content-type: text/html\n\n"; at the top of my code. Yet i have checked there should be no output before i do declare it later in the code.

I want to know what causes output before the call and why.
In the documentation regarding headers it does not say which perl output methods (e.g. print / die etc) will need the header, so aside from Print i am not sure what else to look for.

When CGI.pm talks about sending other types of header is does not give any background to where you can decide which headers to send or what kind of errors you might have if you send the wrong headers.
 
Check the sever error log, I doubt you will find anything useful but you might.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Maybe it's a AJAX problem?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
If it's on your box, you could always install WireShark and have a look at the actual traffic being sent to your browser. It's crude and unsophisticated, but...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
RE: Trav69, yes it all closely tied into the ajax, but i am actually using prototype (the library) and i am trying to get the script to give a status code so that the ajax uses "onSuccess" and "onFailure" appropriately.

RE: stevexff I will look into that.

I am in two minds about AJAX, it allows for cool stuff, but it is changing the way to development.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top