tags:

views:

75

answers:

1

Hi,

When i submit a FORM on my page, my "response text" returns the HTML of the entire page and not only the FORM that submitted. This is normal?

+1  A: 

responseText is XHR speak for "What the server responds to the request with, excluding the HTTP headers".

It is perfectly normal for it to include a complete HTML document if you access a resource designed to respond to a regular form submission.

It sounds like you need to be smarter about what the server responds with.

This simple example (written in Perl, see the link for more context) checks a query string parameter to decide if it should place the data it has fetched in an HTML template and return it, or to convert the data to JSON and return that instead.

  if ($view eq "json") {
    my $data = $json->convert_blessed->encode($vars);
    print $q->header('application/json;charset=utf-8'), $data;
    return;
  }

  my $output;
  $tt->process('html.tt', $vars, \$output)
    || die $tt->error(), "\n";

  print $q->header('text/html;charset=utf-8'), $output;
David Dorward