0

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

Augusto
  • 779
  • 5
  • 18
nitte93
  • 1,820
  • 3
  • 26
  • 42
  • checkout this related post. http://stackoverflow.com/questions/6114436/access-control-allow-origin-error-sending-a-jquery-post-to-google-apis – xspydr Jan 24 '14 at 17:31

3 Answers3

1

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.

KiraLT
  • 2,385
  • 1
  • 24
  • 36
  • Thanks Lukas ...Thanks already looked through it..i am still unable to get it ..i mean how is that a croww_policy_origin issue.all my references are pointing to the same domain and non of my links are pointing to any cross sites.And if thats really the issue then how come its working for the post() function. Thanks for your response. – nitte93 Jan 24 '14 at 17:45
1

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.

A.T.
  • 24,694
  • 8
  • 47
  • 65
  • HI..Aaron thanks for your valuable comment..but here i am reffering to the same domain name..i am not using any cross domain here.. – nitte93 Jan 24 '14 at 17:37
  • And my both files are hosted on the same server and domain name. – nitte93 Jan 24 '14 at 17:57
  • which browser you are using, check there base urls – A.T. Jan 24 '14 at 17:58
  • i am using chrome.. but I checked it for other browsers also. and i am getting the error there also.there base urls are same. – nitte93 Jan 24 '14 at 18:03
  • did you try this url http://www.nizilla.tk/profile php/userinterest.php there is space in profile and php, it is not valid url – A.T. Jan 24 '14 at 18:07
0

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

Augusto
  • 779
  • 5
  • 18
  • thanks Augusto i really appreciate your help! so what am i doing wrong here?..because i have not used any cross domain here, and all my links are pointing to the same domain name.so i dont think that there is any chance you same _origin_policy. – nitte93 Jan 24 '14 at 17:41
  • Hi Augusto.. I went through your blog, it's really well written.. Butas it says "One thing to remember is that the query will allow you to load resources only from your site. That is, you cannot tell jQuery to tell a page/fragment from another site." but that's exactly my doubt here as all my jquery links and other links are pointing to the same domain name and server. So how is that an issue here. – nitte93 Jan 24 '14 at 18:00
  • I'm glad you enjoyed it. Then, if you're sure all your requests point to the same site, use relative path, like profile php/userinterest.php – Augusto Jan 24 '14 at 18:07
  • Thanks for replying.. I tried it also and still the same error pops up....i am getting no error only when i remove the "" comletely. – nitte93 Jan 24 '14 at 18:22
  • Just to add more information to your problem, I googled your problem and some results came back. I read the first three or four, and they were problems relating to the hosting service. This is what I googled -> http://redirect.main-hosting.com/error404.php/ Maybe this is entirely unrelated. From what you say, I can tell that the problem is not the load function, but the jquery library, that the browser is not being able to download. – Augusto Jan 24 '14 at 18:51