2

I am using the jQuery post method from : http://www.w3schools.com/jquery/jquery_ajax_get_post.asp

$("button").click(function(){
    $.post("demo_test_post.pl",
    {
        name: "Donald Duck",
        city: "Duckburg"
    },
    function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });
});

The server side script is in perl. The print statements in perl are getting in the data parameter of the callback function. And I am always getting status as "success"

Basically I need to propagate an error condition to the front-end which I am not able to simulate. I have tried die, exit and return with different values but in all cases I get status as "success".

How can I get the non-success status for this jQuery function?

Also Is there a cleaner way to receive the error condition from the back-end perl server?

Below is the perl code on the server side:

#!/usr/bin/env perl
#|++

use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $q = new CGI;

main();

sub main()
{
  if($q->param())
  {
     my $name = $q->param('name');
     my $city = $q->param('city');

    print('You entered'.$name.' and '.$city);
  }
}

With above code I have experimented with return, die & exit but I always get success as status.

Scorpious
  • 133
  • 1
  • 9
  • 2
    You'll need to show some Perl code if you expect us to have any idea what's going on. – Matt Jacob Feb 27 '16 at 15:33
  • 1
    See https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html , http://stackoverflow.com/questions/15656514/return-http-error-code-from-cgi-c-module – guest271314 Feb 27 '16 at 15:41
  • See http://api.jquery.com/jQuery.post/ it might be useful to you – Harish Kommuri Feb 27 '16 at 15:52
  • @Matt added the server side perl code & guest271314 also experimented with print("HTTP/1.1 400 Bad Request\n"); but no luck yet. – Scorpious Feb 28 '16 at 03:38
  • @Scorpious What is response to ajax using `print("HTTP/1.1 400 Bad Request\n");` ? Have you added `.fail(function(jqxhr, textStatus, errorThrown) { console.log(textStatus, errorThrown)})` chained to `$.post()` to handle error response ? – guest271314 Feb 28 '16 at 04:00
  • @guest271314 I have tested using the status string, it comes to success all the time. – Scorpious Feb 28 '16 at 05:03
  • Have you tried `die "Bad error here";` http://perldoc.perl.org/CGI/Carp.html#MAKING-PERL-ERRORS-APPEAR-IN-THE-BROWSER-WINDOW ? – guest271314 Feb 28 '16 at 05:46

2 Answers2

1

If you want to test a failed Ajax call, your Pearl code should return a 400, ..., 403, 404 or 500 HTTP-statuscode.

Francis Chartrand
  • 167
  • 1
  • 1
  • 15
0

The status returned by jQuery's post function is the HTTP-statuscode. Everytime you print something in your script it will be sent to the browser by your webserver with 200 OK Status.

Now one of your options is to indicate the error in the responded data. Example using a json-encoded server-response:

{
    "error": true
}

One other option is to set the HTTP-Statuscode to something else (for your case the code 400, which means 'Bad Request' could be appropiate).

nam3less
  • 1
  • 2
  • fix your syntax in json: use not "=", but ":" for separating key from value. And "," after last key-value pair is syntax error. – Nikolai Saiko Feb 27 '16 at 15:54