EcStart PHP 技術討論論壇's Archiver

anichiu 發表於 2007-4-23 23:37

如果使用ajax抓回的xml,其中一個欄位沒有資料,導致錯誤

請問各位前輩
如果使用ajax抓回的xml,如果其中一個欄位沒有資料,雖然其它的資料依然可以秀出,可是卻會產生錯誤訊息,該如何解決

FIEND 發表於 2007-4-24 08:48

其中一個欄位沒有資料,雖然其它的資料依然可以秀出 ? 有點深 呵呵.....

anichiu 發表於 2007-4-24 12:46

對不起~大大...沒將問題表明清楚..
假設,有使用ajax方式的主頁a.html
有資料要被抓取的子頁test.xml

其中 test.xml中的資料標籤為
<response>
<ltel>木子李</ltel>
<lmtel>712345678</lmtel>
<laddress></laddress>
</response>
抓取資料回母頁時,畫面秀出正常,唯laddress標籤內的資料是空值
會導致母頁左下角出現javascripts的錯誤...

不知道有沒有方法可以避免..

anichiu 發表於 2007-4-24 12:49

又...這個錯誤是如何導致的...問過朋友,他也一樣有出現這個問題,目前的解決方法是在網頁輸出至client前,先用php判斷是否有空值,如果是就先代入一個值,再自行用javascripts處理...

小弟想知道有沒有更簡單,直接在javascripts中就可以處理的方法...

FIEND 發表於 2007-4-24 16:48

你的程式長什麼樣子.?

JS 的部份 .

我只能猜測 .  因為你沒有先宣告變數.

所以系統會 因為你直接呼叫了這個變數而產生錯誤

所以你要在判斷沒有這個變數的情況下我要給一個空值或先行宣告才不會有錯誤.

上述用 PHP 是一個解法  , JS 當然也能解你沒有給我程式碼我沒法子回答你只能告訴你方向.

anichiu 發表於 2007-4-24 22:47

我的JS很普通..
var http_request = CreateXmlRequestObject();

function CreateXmlRequestObject(){
        http_request = false;
        if (window.XMLHttpRequest){ // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
        if(http_request.overrideMimeType){
                        http_request.overrideMimeType('text/xml');
        }
    }else if(window.ActiveXObject){ // IE
                try {
                        http_request = new ActiveXObject("Msxml2.XMLHTTP");
              }catch(e){
                        try{
                                http_request = new ActiveXObject("Microsoft.XMLHTTP");
                        }catch(e){
                                alert('Giving up :( Cannot create an XMLHTTP instance');
                        }
          }
        }
               
    if(!http_request){
                alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }else{
                return http_request;
        }
}

function ProcessAJAX(Method,url){
        if(http_request){
                try{
                        http_request.onreadystatechange = Getxml;
                        http_request.open(Method,url,true);
                http_request.send(null);
                }catch(e){
                        alert("Can't connect to server");
                }
        }
}

function Getxml(){
        if(http_request.readyState == 4){
                if(http_request.status == 200){
                        var XmlObj = http_request.responseXML;
                        var root_node = XmlObj.documentElement;
                        name1.innerText = root_node.getElementsByTagName("lte")[0].firstChild.data;
                        tel.innerText = root_node.getElementsByTagName("lmtel")[0].firstChild.data;
                        addr.innerText = root_node.getElementsByTagName("laddress")[0].firstChild.data;
                }else{
                        alert('無法取得資料');
                }
        }
}

anichiu 發表於 2007-4-24 22:49

執行ProcessAJAX(Method,url)
xml檔,如上

anichiu 發表於 2007-4-26 13:07

大大????您看到我貼的文了嗎??

頁: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.