php 如何通过 jQuery AJAX 请求输出响应 HTML 数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15633349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:39:38  来源:igfitidea点击:

How to output the response HTML data by a jQuery AJAX request?

phphtmljqueryresponseoutput

提问by Tomkay

I have an online shop with a shopping cart. The cart, which is a <table>, refreshes its content after adding an article to the cart.

I use jQuery's AJAX method which receives HTML <td><tr>as a response from the called PHP script. Firebug's console shows the correct responsefrom the call.

As you can see in my code, I want to add HTML to the table. I can't get it to work. Do I not understand the AJAX method? I want to add these <td><tr>to the shopping cart table.

我有一个带购物车的网上商店。购物车是<table>,在将文章添加到购物车后刷新其内容。

我使用 jQuery 的 AJAX 方法,它<td><tr>从被调用的 PHP 脚本接收 HTML作为响应。Firebug 的控制台显示调用的正确响应

正如您在我的代码中看到的,我想将 HTML 添加到 table。我无法让它工作。我是不是不明白 AJAX 方法?我想将这些添加<td><tr>到购物车表中。

JavaScript (Using jQuery 1.9.1)

JavaScript(使用 jQuery 1.9.1)

$.ajax({
    url: 'php/addToShoppingCart.php',
    type: 'POST',
    dataType: 'html',
    data: content,

    complete: function(data) {  
        $('#shop section table tbody').append(data);
    },
});


Firebug console

萤火虫控制台

enter image description here

在此处输入图片说明

回答by Stefan Candan

have tried it using .done()?

使用 .done() 尝试过吗?

$.ajax({
  url: 'php/addToShoppingCart.php',
  type: 'POST',
  dataType: 'html',
  data: content,
}).done(function ( data ) {
  $('#shop section table tbody').append(data);
});

回答by Devesh

You can use the success also

你也可以使用成功

  $.ajax({
       url: 'php/addToShoppingCart.php',
       type: 'POST',
       dataType: 'html',
       data: content, 
       success : function(data) 
      {$('#shop section table tbody').append(data);}
      });

回答by Mahendra

Well inside your cart create table and one tr so that you can display titles at the top

在您的购物车内创建 table 和一个 tr,以便您可以在顶部显示标题

<table cellpadding="5" cellpadding="2" border="0" id="cartprod">
    <tr>
      <td width="60%"><b>Product Name</b></td>
      <td width="20%"><b>Quantity</b></td>
      <td width="20"><b>Price</b></td>
    </tr>
</table>

Then you can append your content like this

然后你可以像这样附加你的内容

function AddToCart(pid)
{
    $.post('php/addToShoppingCart.php',{pid:pid},function(data){    
       $('#cartprod tr:first').after(data); 
    },'html')
}

回答by Yogesh Suthar

The syntax for completeis

的语法complete

$.ajax({
url: 'php/addToShoppingCart.php',
type: 'POST',
dataType: 'html',
data: content,

}).complete(function (data) {
    $('#shop section table tbody').append(data);

});