Could jQuery .post() stop trying to enforce the "guessed" dataType?

41 Views Asked by At

I have the following setup:

The main web project consists of a large(ish) PHP script with a HTML table as its central part. The last table row is intended to be able to add copies of itself. For this purpose I put it into a separate Smarty template file:

<table>
  <tr>
    <td>key row 1</td>
    <td>still row 1</td>
    <td>another row 1 <div>with some Div here</div></td>
  </tr>
  {include "new-row.tpl"}
</table>

new-row.tpl (more or less):

{strip}
<tr>
  <td onBlur="newRow()">key row x</td>
  <td>still row x</td>
  <td>another row x <div>with some Div here</div></td>
</tr>
{/strip}

newRow():

function newRow() {
  $.post(
    "/add-new-row.php", // only displays the new-row.tpl
    {
      param: "doit"
    },
    function(data) {
      alert(data);
      $("table tr:last").after(data);
    }
  );
}

Inspecting what's happening with Firebug's network monitor shows me that data actually gets the right response (the complete <tr>), but suddenly the whole table structure disappears from data when trying to alert it, leaving only the content alive.

I found out that jQuery seems to parse the AJAX-retrieved HTML code. As it doesn't contain a <table> tag, jQuery discards all table structure elements before passing them. I have to explicitly state that my dataType is text; when I do so, everything is fine.

Is this expected behavior or can I, somehow, tell jQuery to take my valid HTML excerpt as a valid HTML excerpt even if it's only a bare table row?

Thanks!

0

There are 0 best solutions below