JelleP
JelleP

Reputation: 1004

Jquery AJAX problem

I have a strange problem with Jquery AJAX. I get the following message at Ie:

Acces denied. jquery-1.4.4.min.js

The code i run is:

 $(document).ready(function(){

   leuk(<?php echo $_POST['decrease_id']; ?>,<?php echo $_POST['user_id'] ?>);

     var int=self.setInterval("leuk(<?php echo $_POST['decrease_id']; ?>,<?php echo $_POST['user_id'] ?>);",5000);

    });


     function leuk(decrease_id,user_id)
     { 

    $.ajax({
     type: "POST",
     url: 'http://schoolprove.nl/nieuw/index.php/leerlingen/checkvoortgang/',
     data: 'decrease_id='+decrease_id+'&user_id='+user_id,
     success: function(msg){
      $('#output').html(msg);
      document.getElementById('opnieuw').style.display = 'none';
    }
    });

    } 

It is just a simple AJAX request but i don't know why this occured. I did this reqeust several times but this at this page this error always occured and the AJAX request does not work.

Does anybody know how to solve this.

Thank you very much!!!!

Upvotes: -1

Views: 98

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1074038

If your document page isn't also being loaded from http://schoolprove.nl, you're running into the Same Origin Policy restriction.

You have very few options if you want to POST data cross-origin:

  • If you're in control of the server and your users can stick to recent versions of Firefox, Chrome, or a few others, you can use the new CORS standard. That doesn't work at all with versions of IE prior to IE8, and in IE8 it doesn't work with jQuery's ajax stuff because Microsoft decided to use a different object (XDomainRequest instead of XMLHttpRequest).
  • Otherwise, you'll need a server-side proxy that you send the request to (obeying the SOP) and then have a server resource actually post to schoolprove.nl (edit: Tim Cooper's given you a very basic example of that in PHP).

You'd have more options with a GET, including:

...but my guess is that a GET isn't appropriate for what you're doing, as it isn't idempotent. (If it is, then that's what I'd do.)

Upvotes: 0

user142162
user142162

Reputation:

You cannot make AJAX requests that are to a different domain than the one the script is currently on. Perhaps you can make a script on your server which issues the cross-site request, if necessary.

An example of the script that would be on your server:

$curl_res = curl_init("http://schoolprove.nl/nieuw/index.php/leerlingen/checkvoortgang/");

curl_setopt($curl_res, CURLOPT_POST, true);
curl_setopt($curl_res, CURLOPT_POSTFIELDS, "decrease_id=" . $_POST[ 'decrease_id'] .  "&user_id=" . $_POST['user_id']);

curl_exec($curl_res);

Upvotes: 1

Georgi
Georgi

Reputation: 395

You are making AJAX request outside your domain. This is browser limitation and applies to all browsers.

Upvotes: 1

Related Questions