0
我想從div獲得一些價值並將其發送到數據庫,我寫它,它在我的本地主機上工作,但當我上傳源我的主人它停止工作...Proplem與獲取元素ID(未捕獲TypeError:無法設置屬性'innerHTML'爲空)
這個錯誤我在控制檯中看到同一頁上
Uncaught TypeError: Cannot set property 'innerHTML' of null index.php:700
request.onreadystatechange
阿賈克斯那裏的div是
<script type="text/javascript">
// create the XMLHttpRequest object, according browser
function get_XmlHttp() {
// create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)
var xmlHttp = null;
if(window.XMLHttpRequest) { // for Forefox, IE7+, Opera, Safari, ...
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject) { // for Internet Explorer 5 or 6
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlHttp;
}
// sends data to a php file, via POST, and displays the received answer
function ajaxrequest(php_file, tagID) {
var request = get_XmlHttp(); // call the function for the XMLHttpRequest instance
// create pairs index=value with data that must be sent to server
var the_data = 'pg='+document.getElementById('template').innerHTML;
var the_data2 = 'memid='+document.getElementById('memid').innerHTML;
var the_data3 = 'proid='+document.getElementById('proid').innerHTML;
var the_data4 = 'taipei='+document.getElementById('taipei').innerHTML;
request.open("POST", php_file, true); // set the request
// adds a header to tell the PHP script to recognize the data as is sent via POST
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var post_data = the_data + '&' + the_data2 + '&' + the_data3 + '&' + the_data4;
request.send(post_data); // calls the send() method with datas as parameter
// Check request status
// If the response is received completely, will be transferred to the HTML tag with tagID
request.onreadystatechange = function() {
if (request.readyState == 4) {
document.getElementById(tagID).innerHTML = request.responseText;
}
}
}
</script>
的div
<div id="template"> some html here </div>
<div id="memid" style="display:none;">4</div>
<div id="proid" style="display:none;">55</div>
<div id="taipei" style="display:none;">sav</div>
按鈕
<button value="sasasasasa" onclick="ajaxrequest('temp1.php', 'context')"></button>
那段代碼中的哪行是700行?當你通過內置在瀏覽器中的調試器來瀏覽時,你會看到什麼? – 2013-02-16 14:22:30
爲什麼它被標記爲java和jquery-ajax,因爲你不使用java和jquery? – 2013-02-16 14:22:31
可能值得一提的是,在向服務器發送URI編碼數據時(例如,在你的'the_data','the_data2'等等變量 - 技術上''='之前使用的名稱'),你確實需要使用'encodeURIComponent'。 '也是如此,但是你只能使用26個英文字母,數字和其他幾個字符,而不用簡單的名字就可以離開。 – 2013-02-16 14:28:46