我正在尝试根据用户兴趣创建一个简单的新闻源页面。 下面我提供了相同的代码..我只是不明白这里的问题..问题是相同的代码在我的本地主机上工作正常,但在在线服务器上工作不一样。但是 $(window) .scroll(function() 工作正常,数据正在正确获取,但 .load() 函数无法获取数据。
我在 javascript Consol.log 中遇到以下错误
XMLHttpRequest cannot load http://redirect.main-hosting.com/error404.php/26?domain=www.nizilla.tk. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.nizilla.tk' is therefore not allowed access.
我还尝试将 src 路径更改为 src="./profile-newsfeed/jquery-1.9.1.min.js"但我仍然面临同样的错误。
/*-------------------------------- */
<script src="http://www.nizilla.tk/profile-newsfeed/jquery-1.9.1.min.js"> </script>
<script type="text/javascript">
$(document).ready(function() {
var track_load = 0; //total loaded record group(s)
var loading = false; //to prevents multipal ajax loads
var total_groups = <?php echo $totalpage; ?>; //total record group(s)
//alert(total_groups);
if(total_groups<=0)
{
window.location='http://www.nizilla.tk/profile php/profilefollow.php';//
}
else
{
$('#container').load('http://www.nizilla.tk/profile php/userinterest.php', {'group_no':track_load}, function() {track_load++;}); //load first group
$(window).scroll(function() { //detect page scroll
if($(window).scrollTop() + $(window).height() == $(document).height()) //user scrolled to bottom of the page?
{
if(track_load <= total_groups && loading==false) //there's more data to load
{
loading = true; //prevent further ajax loading
$('.animation_image').show(); //show loading image
//load data from the server using a HTTP POST request
//http://www.nizilla.tk/profile php/userinterest.php
$.post('http://www.nizilla.tk/profile php/userinterest.php',{'group_no': track_load}, function(data){
$("#container").append(data); //append received data into the element
//hide loading image
$('.animation_image').hide(); //hide loading image once data is received
track_load++; //loaded group increment
loading = false;
}).fail(function(xhr, ajaxOptions, thrownError) { //any errors?
alert(thrownError); //alert with HTTP error
$('.animation_image').hide(); //hide loading image
loading = false;
});
}
}
});
}
});
如果您能在这里指出我的错误,那将非常有帮助......我非常感谢您的帮助
最佳答案
如果我理解正确的话,你正在做 XMLHttpRequest到与您的页面所在的域不同的域。因此,浏览器会阻止它,因为出于安全原因,它通常允许同一来源的请求。当你想要进行跨域请求时,你需要做一些不同的事情。有关如何实现这一目标的教程是 Using CORS 。
当您使用 postman 时,他们不受此政策的限制。引自 Cross-Origin XMLHttpRequest :
Regular web pages can use the XMLHttpRequest object to send and receive data from remote servers, but they're limited by the same origin policy. Extensions aren't so limited. An extension can talk to remote servers outside of its origin, as long as it first requests cross-origin permissions.
关于Javascript .load() 函数未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21338771/