簡體   English   中英

通過URL發送json數據還是使用$ .post()?

[英]Send json data through URL or use $.post()?

不知道哪個更有效。 我想在另一頁上獲取產品的所有數據。 我對ajax比較陌生。 這是顯示產品網格的函數,當單擊產品時,它將轉到新頁面,其中包含更多詳細信息:

function ajaxfunction(json_data){
    var path = "images/products/shirts/smallthumbs/";
    var url = "Shirtbuy.php";
    var table = $("<table></table>");

 //array iterates through json array

    for (var i = 0; i < json_data.length ; i++){
     if (i %4==0)
     var tr = $("<tr></tr>").appendTo(table);         
     $(tr).append('<td width="500">' + json_data[i].prod_name + '<br/>' +

 //See below, sending all the data through the url seems really messy. Does ajax.post achieve the same thing ?

        '<a href="' + url +"?id="+ json_data[i].product_id + "&price=" +json_data[i].price+ '"><img src="' + path + json_data[i].pic + '"/></a>' + '<br/>' + 
        '\u00A3' + json_data[i].price + '</td>');
    }       

$("#maindisplay").append(table);
}

如果操作是冪等的,請使用其他方法,如果該操作具有持久的結果,請使用post,因為刷新可能會導致操作兩次。此外,還存在其他一些需要考慮的問題,例如,如果要緩存結果頁面。 Teamtreehouse有一個不錯的博客文章,您應該考慮。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM