0

我有

<div id="infoMessage" class="infoMessage"><?php echo $message;?></div>

$message 将在 POST 时从服务器生成。

如果我想在 ajax 成功后显示 infoMessage 我该怎么做

 $.ajax({
   type: "POST",
   url: "the/form",
   data: $(\'.target\').serialize(),
   success: function() {
    alert("done");
    // how to show the infoMessage ?
    }
  });
4

3 回答 3

0
 $.ajax({
   type: "POST",
   url: "the/form",
   data: $(\'.target\').serialize(),
   success: function(data) { // data is a callback variable which stores info echoed in PHP file during transfer, so you have to $message there to get it here
    alert(data); // this will alert it
    }
  });

在这里,您可以使用任何变量而不是函数(数据)来存储 AJAX 响应

于 2012-12-27T03:47:07.480 回答
0

尝试使用以下代码而不是您拥有的代码。

$.ajax({
   type: "POST",
   url: "the/form",
   data: $(\'.target\').serialize(),
   success: function(data) {
    alert(data); //data contains the ajax response
    $("#infoMessage").html(data); //you set here where to ajax response will be displayed
    }
  });
于 2012-12-27T03:48:16.413 回答
0
$.ajax({
   type: "POST",
   url: "the/form",
   data: $(\'.target\').serialize(),
   success: function(data) {
    //alert("done");
    $('#infoMessage').html(data);
    }
  });

您必须在 ajax 文件(/表单)中回显消息

于 2012-12-27T03:49:01.180 回答