2015-12-26 106 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,但不是当我从索引 –