Perl中的Ajax与Dol

Anyone can tell me what I'm doing wrong? I am creating a simple system to get people in and out of user groups and for that purpose I am using Dojo and Perl. (If I could have it my way it would be PHP but I am not the boss.)

At the moment I only use three files, one for Perl, one for JavaScript and one for CSS styles.

The start of the CGI script routes to different functions as follows:

if ($search = $cgi->param('psearch')) {
  dbConnect();
  jsonSearchPersons($search);
  dbDisconnect();
} elsif ($user_id = $cgi->param('person')){
  dbConnect();  
  create_form($user_id);
 dbDisconnect();
} elsif ($user_id = $cgi->param('saveuser')) {
  save_user();
} else {
  mainPage();
};
...
sub save_user {
  print $cgi->header(-type=>'text/plain',-charset=>'utf-8');
  print("success");
}

The problem I have now is when I want to save the new groups for the user though an Ajax call (a call to this URL: users.cgi?saveuser=xx). This should (in my point of view) be a POST call, so I made this and tried to append the resulting HTML/text in a <div> but it didn't work:

dojo.xhr.post({
  url: "/cgi-bin/users.cgi?saveuser="+user_id,
  content: {
    new_groups: group_ids.toString()
  },
  load: function(html_content){
    var element = document.getElementById("test_area");
    element.innerHTML = html_content;
  },
  error: function(){
    alert("An error has occured during the save of new user groups.");
  }
});

When I do it with dojo.xhr.get(); it works fine, but when I do it with the POST it's like it jumps over that part of the if statement and just appends the mainPage() function. Is there something basic I don't understand between Dojo and Perl? Do I have to set up the pages so it will accept a POST call? Or what am I doing wrong?

NOTE: This is the first "system" I have made though Dojo and Perl. (I'm normally a PHP/jQuery kind of guy who makes everything UI by hand, so I'm kinda new to it.)

Try adding the saveuser-parameter to the content-object of dojo.xhrPost instead of passing it in the url.

You're trying to pass the saveuser-parameter as GET and the other as POST, maybe that confuses your serverside part.

Try it like that:

dojo.xhr.post({
  url: "/cgi-bin/users.cgi",
  content: {
   new_groups: group_ids.toString(),
   saveuser: user_id
  },
  load: function(html_content){
    var element = document.getElementById("test_area");
    element.innerHTML = html_content;
  },
  error: function(){
    alert("An error has occured during the save of new user groups.");
  }
 });

Line 675 of CGI.pm :

  # Some people want to have their cake and eat it too!
  # Uncomment this line to have the contents of the query string
  # APPENDED to the POST data.
  # $query_string .= (length($query_string) ? '&' : '') . $ENV{'QUERY_STRING'} if defined $ENV{'QUERY_STRING'};

Made me laugh !

Found a solution.

The problem was my javascript. When posting to a perl script you use $cgi=new CGI; and all that. This takes both GET and POST variables and validates them. In my javascript/dojo code, i then used an url with GET vars and then made a POST as well. This meant perl could not find out (or was mixing) the two variable types. So when i changed my ajax code (as below) it worked, since $cgi->param('saveuser') both fetches GET and POST of "saveuser" (no change to the perl was needed):

dojo.xhr.post({
  url: "/cgi-bin/users.cgi",
  content: {
    saveuser: user_id,
    new_groups: group_ids.toString()
  },
  load: function(html_content){
    var element = document.getElementById("test_area");
    element.innerHTML = html_content;
  },
  error: function(){
    alert("An error has occured during the save of new user groups.");
  }
});

Kinda wack bug, but im glad since it works great now :D