0

I have an ajax call which returns the following HTML:

<form action="/Function.aspx/Add" method="post">
    <input id="Type" name="Type" type="hidden" value="Customer" />
    <input id="CustomerId" name="CustomerId" type="hidden" value="3" />


    <tr>
        <td>
            <input type="submit" value="Save" style="width:7em;" />
        </td>
        <td colspan="2">
            <select id="CustomerAppId" multiple="multiple" name="CustomerAppId">
    <option value="BA">Option 1</option>
    <option value="AB">Option 2</option>
    <option value="DC">Option 3</option>
       </select>
        </td>
    </tr>

</form> 

The ajax script performs great, and my result looks good in the alert() call:

<table id="appTable">
<tr>
    <th></th>
    <th><label for="AppId">ID</label></th>
    <th><label for="AppName">Description</label></th>
</tr> 

<tr>

    <td>  
    <a href="/Function.aspx/Remove/221?reqId=3">Delete</a>
    </td> 

    <td>PSE</td>
    <td></td>
</tr>

</table>        

<a href="/Function.aspx/BlankEditorRow/3?type=Customer" id="addItem">Add another...</a>

<script type="text/javascript">
$("#addItem").click(function () {
    $.ajax({
        url: this.href,
        cache: false,
        success: function (html) {

            alert(html);
            $("#appTable").append(html);
        },
    });
    return false;
});
</script>

However, my HTML never shows up.
I tried:

  • Replacing the html variable in the script to '<tr><td>Hello!</td></tr>': that works.
  • Removing all and tags, leaving the plain and contents: that did NOT work.
  • Added $(document).ready() to the beginning of the script: No change

I've been staring at this HTML trying to figure out what it doesn't like, and I'm just not seeing it. Any ideas?

10
  • 3
    You're missing a <table> element. That might be the problem. Commented Mar 21, 2011 at 19:34
  • 1
    Do You have $(document).ready(function() {//script here?...}) Commented Mar 21, 2011 at 19:34
  • As Frederic says, the html you are trying to insert is not valid without a table element, hence why a plain string works but this doesn't Commented Mar 21, 2011 at 19:37
  • 1
    where is the HTML element with id=appTable? Commented Mar 21, 2011 at 19:38
  • Hehe - the table is elsewhere on the page. I didn't think to include that, since I mentioned that adding just a plain '<tr><td>Hello!</td></tr>' works. I'll update the question to show it, however. Commented Mar 21, 2011 at 19:51

1 Answer 1

1

Apparently, in order to properly evaluate the data from an ajax call, you need to wrap the result in a $().

So, when I changed this line:

$("#appTable").append(result);

To this:

$("#appTable").append($(result));

The row was added successfully.

Sign up to request clarification or add additional context in comments.

2 Comments

The answer I posted worked for me and resolved the problem I stated above...perhaps there is a difference in the issue that you are encountering? Reading the comments posted to the original question might provide some guidance.
You are right. I did not use append() properly. I am using after() now, and it works properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.