Javascript .load() 函数未执行

标签 javascript php jquery ajax

我正在尝试根据用户兴趣创建一个简单的新闻源页面。 下面我提供了相同的代码..我只是不明白这里的问题..问题是相同的代码在我的本地主机上工作正常,但在在线服务器上工作不一样。但是 $(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/

相关文章:

php - 日期列在提交给数据库的表单上未显示正确的日期戳

javascript - 如何使用 HTML5 或 JavaScript 查看 RTSP 流,而不使用 Real Player 插件上的 VLC 插件等插件?

javascript - 如何通过GLSL在THREE.js中实现MeshNormalMaterial?

php - 将动态 PHP 表单插入 MySQL

php - 将结果内爆为来自 mysqli_fetch_assoc 的 block ,其中 perf1_id 不能位于同一 block 中

javascript - 如何在其中包含特定元素的第一个元素之前或之后插入?

javascript - HTML 按钮操作在 Safari 中不起作用

javascript - 你能有一个javascript条件正则表达式语句吗?

javascript - jQuery-mobile 和 ASP.NET 组合问题

javascript - selectElement.value 不起作用