2016-09-28 42 views
2

在這裏,我從數據庫中獲取JSON格式的數據到我的.js文件中,並且我也在使用div。但是我沒有在我的HTML中獲取div page.Please誰能告訴我如何在我的HTML page.Below顯示是我的代碼:如何顯示js文件在html頁面內的js文件div

我的JSON數組:

[{"chat_question_id":"1", 
"chat_question_title":"What is PHP?"}, 
{"chat_question_id":"17", 
"chat_question_title":"what is php?",} 

下面是我的js代碼:

function ChatQuestionsInfo(bRowId) 
    { 
    var actionType = ""; 
    var hdnFlagForSearchQue = $("#hdnFlagForSearchQue").val(); 
    if(hdnFlagForSearchQue=="insert"){ 
     actionType = "ChatQuestionsInfo"; 
    } else { 
     actionType = "searchQuestiontitle"; 
    } 
    if($.trim($("#questionname").val())==""){ 
     $("#questionname").focus(); 
     alert("Enter Question Name"); 
     return false; 
    } 
    if($.trim($("#technologytags").val())==""){ 
     $("#technologytags").focus(); 
     return false; 
    } 
    $.post(rootUrl+"includes/ajax/ajax_chat.php", {action: actionType,bRowId:bRowId,bQuestionName: $.trim($("#questionname").val()),bTechnologyTags: $.trim($("#technologytags").val())}, 
    function(data){ 
     var htmlText = ''; 
    for (var key in data) { 
      htmlText += '<div class="tab-content">'; 
      htmlText += '<div id="newquestions"> : ' + data[key].chat_question_title + '</div>'; 
      htmlText += '</div>'; 
     } 
     $('.chat_body_form').append(htmlText); 
    }, "json"); 
    return false; 
    } 

HTML代碼:

<div id="newquestions"></div> 
+0

警報工作? –

+0

不,它不起作用 – priya

+0

在控制檯中打印您的對象並顯示我 like'console.log(chat);'將在控制檯窗口中打印 –

回答

0

我認爲你需要做2個修改,1是在ajax_chat.php文件中。和第二在你的JavaScript。

  1. 每個JSON輸出都需要純JSON輸出,所以javascript(或jQuery)可以輕鬆讀取它。所以在ajax_chat.php文件的輸出上,你必須設置一個頭來給出正確的輸出內容類型。

例如

header('Content-Type:application/json'); 
echo json_encode('your varialbe array'); 
  • jQuery中最好是使用$。每然後for循環,並且還可以在直接追加數據循環。
  • 例如

    function(data){ 
    $.each(data,function(i){ 
        $('.chat_body_form').append('<div class="tab-content"><div id="newquestions"> : '+ $(this).chat_question_title + '</div></div>'; 
    });  
    } 
    

    我認爲這些變化會工作,也將是非常簡單和乾淨的代碼來了解或更改,

    謝謝...

    相關問題