in reply to Re: CGI Header Breaks on Second AJAX Call
in thread CGI Header Breaks on Second AJAX Call

I did not want to leave the thread dead, and I wanted to make sure I got back to everyone here.

I did heed your advice and used cURL to break this down as much as I could. I also tried to cut down the process to a minimal reproducible call. The results were even more perplexing to me.

my $json = JSON->new->utf8->encode($data); my $q = $self->query; print $q->header( -type => 'application/json', -charset => 'utf-8', ); print $json; $self->session->flush if $self->can('session') && $self->session_l +oaded; # Absolutely murder the rest of the call stack exit; # OR die if mod_perl yells at exit()

The code above intercepts my return, spits out exactly what I want, and nukes Perl immediately to prevent any unwanted data manipulation.

The browser then received that JSON as a return object (I checked the network call), and then Chrome or perhaps js wrapped the whole darn thing in HTML as some default behavior Also note the url then redirects/points to my raw Api.pm file path, not my original url login path

<html> <head><meta name="color-scheme" content="light dark"><meta charset="ut +f-8"></head> <body> <pre>Content-Type: application/json {"abbr":"Btest","companyid":7,"email":"bjp@example.com","_resume":"nul +l","name":"Brandon Test","success":1,"userid":1234}{ "error" : "API Error. ModPerl::Util::exit: (120000) exit was called + at /var/www/html/testapp/TestTracker/Api.pm line 1005", "event_threadid" : 21169609 } </pre> <div class="json-formatter-container"></div></body></html>

This is kind of why I'm here and why it has been so hard to ask for help. I don't even know what TO ask or how to break this behavior down more. CGI is a beast I have yet to tame, and something is not happy with this second AJAX call on the same page.

Oh, and for sanities sake, here's my current AJAX call as of now from debugging it a bit. Note the receiving logic in Perl has no issues interpreting this call and using the JSON data sent.

$('#twofactor_form').on('submit', function(event) { var formData = $('#twofactor_form').serialize(); event.preventDefault(); $.ajax({ url: 'api/userLogin', method: 'POST', data: JSON.stringify(formData), dataType: 'json', beforeSend: null, error: function(event) { alert(event.responseJSON.error); //debugger; }, success: function(data) { //Note this never gets tripped ever. //It never reaches the success or failure state, it just spi +ts out that raw html and my url points to the api.pm path. if ( data._resume ) { //console.log("2FA _resume hit"); var resume = JSON.parse(data._resume); window.location.replace(resume.url); } else { //console.log("2FA no-resume hit"); window.location.replace(BASE_URL); } } }); return false; });

Replies are listed 'Best First'.
Re^3: CGI Header Breaks on Second AJAX Call
by Corion (Patriarch) on Apr 14, 2025 at 14:06 UTC

    I think you get the error message because you called exit() in your reduced example. Don't do that.

    If you then still get the HTML with the HTTP headers in the HTML, then maybe try outputting the HTTP status code first:

    print "200 OK\r\n"; print $q->header(-status => '200 OK', ...); ...

    Apache looks for HTTP headers in the output of your script, and if it doesn't find them, it assumes that your script outputs raw HTML (which your browser then turns into more HTML). So if the above works, then maybe something else is outputting a string via print first, or you need to reconfigure Apache to (not?) want the HTTP status code first from your script.

      I wanted to get back to you and everyone here to finally say I solved this mystery.

      This is to date one of the most difficult things I've ever done as a programmer. I have learned a ton, but would quite frankly rather code vehicle physics for games, not debug core http functionality in Perl CGI haha.

      The darn jquery submit handler was nested incorrectly, and submitting the native form functionality and then the ajax submit form functionality. Somehow the two forms would work correctly upon submission, but I'm assuming form 1 would misconfigure the Apache header, so even though form 2 ran correctly, it was configured wrong upon return, thus sharting out a raw hash, even when I specified the headers.

      What made it even harder was the fact that it did NOT double print any statements even though it would double submit. So there was no "2FA hit" followed by another "2FA hit" in my debug logs. The logic in Perl only ran once. Conceptually, I still don't know how to describe exactly what happened, nor do I know how CGI Perl managed to kind of both gracefully handle it, and not handle it at the same time.

      I'm here to learn, so if anyone has any idea what behavior was happening between Apache and CGI Perl, I'd love a breakdown.