-2

i try to update a list with jquery but for some reason the POST function returns wierd objects. any ideas how to retrieve the data from the POST answer?

 var ans = jQuery.post("mysite.com", {name: "John"})
 var cont = jQuery( ans ).find( "#content" );
 alert(cont);

and the page:

<?php
   echo "<div id='content'>";
   echo $_POST['name'];
   echo '</div>';
?>

it alerts [object], when i try to read all the field of the object with:

answ = '';
for( var key in ans ) {
   answ = answ + 'key: ' + key + '\n' + 'value: ' + ans[key];
}
jQuery('#somediv').html( answ );

any ideas how to get what i echoed on the other page?

EDIT: i tried the following as you all suggested:

var ans = jQuery.post("mysite.com", {name: "John"});
jQuery.when(ans).then(function(answ){
var cont = jQuery( answ ).find( "#content" );
alert(cont);
});

and also:

var ans = jQuery.post("mysite.com", {name: "John"}, function(answ){
var cont = jQuery( answ ).find( "#content" ).html(); 
alert(cont);
});

but it still doesnt work. the alert simply doesnt show which means there is something wrong before it.

4

3 に答える 3

0

@dystroyは正しいです。defered を使用することもできます。

var defer = jQuery.post("mysite.com", {name: "John"});
$.when(defer).then(function(ans){
    var cont = jQuery( ans ).find( "#content" );
    // use cont
});
于 2013-10-15T06:53:49.090 に答える
0

以下のコードの代わりに:

var cont = jQuery( ans ).find( "#content" );

次のコードを使用します。

var cont = jQuery( ans ).find( "#content" ).html(); 
 //to get the content use the .html() or .text()
于 2013-10-15T06:53:29.597 に答える