kevtrout
kevtrout

Reputation: 4984

How to stop html header content from being included in jquery post results?

I am using jquery's "$.post()" function to add a list entry to the mysql database, in php.

$(document).ready(function(){
    $(".theSubmit").click(function(){

        var content = $("textarea").val();
        var listn = $("input[name='list_name']").val();
        var groupn = $("input[name='group_name']").val();

        $.post("/secret_url/lists/add_to_list",
            {list_item: content, list_name: listn, group_name:groupn},
            function(html){$("li:last").after(html);});
        });
   });

All that works fine and the new list item is added to the page. However,using firebug, I observe that I get this extra html header information (see below) as well as my returned data. The info I want and render to the page is inside the "li" below. Everything from the doctype after I'd like to strip or prevent from being included since it becomes visible inside a jeditable edit in place input also on the page.

<li>
   <input type="checkbox" name="row" class="checkbox">
   <input type ="hidden" name="id" value="60" class="contentId">
   <div id="60" class="editable">test Item 4</div>
</li>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  
    "http://www.w3.org/TR/html/strict.dtd">
<html>
    <head>
    <title>Checklists</title>
    <link  href="http://checklist.css" rel="stylesheet" type="text/css" />  
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="jquery-ui-1.7.custom.min.js"></script>
    <script type="text/javascript" src="jquery.jeditable.js"></scrip >
    </head>
    <body>

Can I strip the html head content or stop it from being sent back along with the pertinent info?

Upvotes: 1

Views: 385

Answers (3)

jitter
jitter

Reputation: 54605

Just add an

exit;

statement in the file where you output your response data. Directly after you have output all you want as response body.

simplified example

<?php .... do something do process request ... ?>
    <li>
       <input type="checkbox" name="row" class="checkbox">
       <input type ="hidden" name="id" value="60" class="contentId">
       <div id="60" class="editable">test Item 4</div>
    </li>
<?php exit; ?>

Upvotes: 1

Eugenio Mir&#243;
Eugenio Mir&#243;

Reputation: 2428

I'd use $.postJSON instead of $.post because, like in your example, I always forgot to set the type of data the server will respond to your request. JSON type will help you to render the data as nice as you like formatting it on the client side.

Upvotes: 0

chris166
chris166

Reputation: 4807

What do you use? ASP.NET, PHP?

You have to tell your server to stop processing the request after you've written the desired response. It seems that the server continues to add the normal GET/POST content for that URL. (as if you would submit the form without AJAX)

Upvotes: 0

Related Questions