簡體   English   中英

使用 JQuery 接收來自 post() 的響應

[英]receiving response from post() with JQuery

我有一個使用 Ajax(POST 方法)通過普通 Javascript 工作的腳本。 現在我正在嘗試學習 JQuery,因為它看起來更容易,但我對請求的響應有問題

$.post('ajax/wrapper.php', {'desc':$('#description').val()},
        function(data){
            $('#something').html(data);
        });

當我這樣做時,請求已成功發送。 我可以在我的數據庫中看到更改,但響應未顯示在 div#something 中。 我也試過

...
   $('#something').html($(data));
...

提前致謝

你也可以這樣試試

var jqxhr = $.post("ajax/wrapper.php",{desc:$('#description').val()}, function(data) {
      alert("success");
    });

success回調中顯示數據

jqxhr.complete(function(data){
  $('#something').html(data);
});

您可能會使用 $.load

$("#something").load('ajax/wrapper.php', {'desc':$('#description').val()} );

參考: http://api.jquery.com/load/

注釋掉以下行: header("Content-type: text/xml; charset=iso-8859-1")

請參閱本文檔jQuery.ajax中的dataType參數。 您應該使用 text/html MIME 類型返回 HTML,或者將dataType參數設置為“html”:

$.ajax('ajax/wrapper.php', {
    data: {'desc':$('#description').val()},
    dataType: 'html',
    type: 'POST',
    success: function(data){
        $('#something').html(data);
    }
}).then(console.log); // debug

第一個更可取,因為您確實要返回 HTML,而 text/html 是正確的 MIME。

嘗試這個:

$.post('ajax/wrapper.php', {'desc':$('#description').val()},
        function(data){
            $('#something').html(data);
        }, "html");

嘗試:

$('#something').html($.ajax({
  type: "POST",
  url: "ajax/wrapper.php",
  async: false
 }).responseText)

http://api.jquery.com/jQuery.ajax/

暫無
暫無

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

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