2015-12-26 29 views
-1

我使用js在我的應用程序(我使用Dreamweaver和PhoneGap)上顯示一些內容。當我預覽的HTML單獨工作,但是當我加載其他頁面的HTML不要。AJAX和YQL無法使用Dreamweaver(跨域請求)

我收到這條消息對Firefox的安全性:的ReferenceError:requestCrossDomain沒有定義

這是我的HTML

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>jQuery Mobile Web App</title> 
<script src="js/jquery-1.11.0.min.js"></script> 
<script src="js/cross-domain-request.js"></script> 


</head> 
<body> 

<div id="container"> 

<p id="sitename"> http://catedralaltapatagonia.com/invierno/partediario.php? default_tab=0 
</p> 

function codeAddress(){ 
    var elem = document.getElementById("sitename"); 
elem.value = "http://catedralaltapatagonia.com/invierno/partediario.php? default_tab=0"; 
var path =$('#sitename').val(); 

requestCrossDomain(path, function(results){ 
    $('#container').html(results); 
}); 
return false; 
}; 
</script> 
</body> 
</html> 

和我交domain-request.js:

/ JavaScript Document 
// Accepts a url and a callback function to run. 
function requestCrossDomain(site, callback) { 



// Take the provided url, and add it to a YQL query. Make sure you encode it! 
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + 'http://catedralaltapatagonia.com/invierno/partediario.php?default_tab=0' + '"'+' AND xpath="//*[@id=\'meteo_recuadro\']"') + '&format=xml&callback=?'; 






// Request that YSQL string, and run a callback function. 
// Pass a defined function to prevent cache-busting. 
$.getJSON(yql, function(data){ 

     // If we have something to work with... 
if (data.results[0]) { 
    // Strip out all script tags, for security reasons. 
    // BE VERY CAREFUL. This helps, but we should do more. 
    data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, ''); 

    // If the user passed a callback, and it 
    // is a function, call it, and send through the data var. 
    if (typeof callback === 'function') { 
     callback(data); 

    } 
} 
// Else, Maybe we requested a site that doesn't exist, and nothing returned. 
else throw new Error('Nothing returned from getJSON.'); 
}); 

}

一些線索來解決它?

回答

0

我添加行

<script src="js/cross-domain-request.js"></script> 

和JS被加載

1

您的外部JS文件似乎有錯誤,但它未運行。最後的else聲明不正確。試試這個:

/JavaScript Document 
// Accepts a url and a callback function to run. 
function requestCrossDomain(site, callback) { 

// Take the provided url, and add it to a YQL query. Make sure you encode it! 
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + 'http://catedralaltapatagonia.com/invierno/partediario.php?default_tab=0' + '"'+' AND xpath="//*[@id=\'meteo_recuadro\']"') + '&format=xml&callback=?'; 

// Request that YSQL string, and run a callback function. 
// Pass a defined function to prevent cache-busting. 
$.getJSON(yql, function(data){ 

     // If we have something to work with... 
    if (data.results[0]) { 
    // Strip out all script tags, for security reasons. 
    // BE VERY CAREFUL. This helps, but we should do more. 
    data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, ''); 

    // If the user passed a callback, and it 
    // is a function, call it, and send through the data var. 
     if (typeof callback === 'function') { 
     callback(data); 

     } 
    } 
    // Else, Maybe we requested a site that doesn't exist, and nothing returned. 
    else { 
     throw new Error('Nothing returned from getJSON.'); 
    } 
    }); 
} 
+0

仍然一無所獲。該html被命名爲metereolo.html。如果我預覽meterelo.html孤獨的作品,但是當從index.html中調用.... does not。 –

+0

您是否檢查過控制檯以確保兩次加載外部文件? – sideroxylon

+0

當我從預覽開始時加載js,但不是當我從索引 –