nitte93
nitte93

Reputation: 1840

Javascript .load() function is not executing

I am trying to create a simple Newsfeed page based on the users interest. Below I have provided my code for the same.. I just don't understand the problem here.. the thing is this same code works fine on my localhost but its not working as same on an online servers.But the $(window).scroll(function() is working perfectly and the datas are being fetch properly but the .load() function is not able to fetch the datas.

i am gettingbelow error in 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.

I also tried changing the src path as src="./profile-newsfeed/jquery-1.9.1.min.js" but still i am facing the same error.

/*--------------------------------  */

  <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;

            });

        }
    }
});

}
 });

It would be really helpful if you can point out my mistake here..i really appreciate your help

Upvotes: 0

Views: 144

Answers (3)

A.T.
A.T.

Reputation: 26312

possible soultion is CORS...browser does not allow to get data from different domain, its browser security policy. Browsers implement same origin policy.

Either you host both application in same domain or implement CORS to requesting server application.

EDIT :

www.abc.com != abc.com != http://www.abc.com != https://abc.com != https://www.abc.com

browser feel difference there. They are technically different.

"http://www.nizilla.tk/profile php/userinterest.php" also url you are referring is not valid there is space in it.

Upvotes: 1

Augusto
Augusto

Reputation: 819

The load function is not being executed because of the "same origin policy". The resource requested must be located in the same server.

Upvotes: 0

KiraLT
KiraLT

Reputation: 2607

If I understood it right you are doing an XMLHttpRequest to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons. You need to do something different when you want to do a cross-domain request. A tutorial about how to achieve that is Using CORS.

When you are using postman they are not restricted by this policy. Quoted from 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.

Upvotes: 1

Related Questions