2016-02-26 137 views
0

负载数据不能够插入和使用负载数据的代码jQuery的AJAX插入并从数据库

$.ajaxSetup({ 
    // Disable caching of AJAX responses 
    cache: false 
}); 




$(function() { 

    $("#submit").click(function() { 
     var test = $("#txtarea").val(); 
     var dataString = 'question=' + test; 
     if (test == '') { 
      alert("<h3>Please Enter Some Text</h3>"); 
     } else { 
      $("#loading").show(); 
      $("#loading").fadeIn(400).html('<span class="loading">Loading Comment...</span>'); 
      $.ajax({ 
       type: "POST", 
       url: 'q&a.php', 
       data: dataString, 
       cache: false, 
       error: function() { 
        $('#loading').html('<p>An error has occurred</p>'); 
       }, 
       success: function(html) { 
        $("#cm").after(html); 
        document.getElementById('txtarea').value = ''; 
        document.getElementById('txtarea').focus(); 
        $("#load").hide(); 
       } 
      }); 
     } 
     return false; 
    }); 

}); 
+0

更多有关该问题的更多细节 – guradio

+0

试图插入数据并使用此jquery ajax代码获取数据但不工作。 –

+0

我正在尝试做相同的计算器'add comment'部分的作品 –

回答

0

你需要URL编码的数据值,如果它包含特殊字符。最简单的方法是使用对象而不是字符串; jQuery将自动对其进行编码。

var datastring = { question: test }; 

顺便说一句,这通常不需要使用cache: falsetype: "POST"。浏览器不应缓存POST响应,只有GET响应。

另外,在您的success函数中,您隐藏了错误的ID。它应该是$("#loading").hide()

+0

现在代码工作正常。感谢Barmar先生 –

相关问题