0

i have a simple setup, with php pulling html content from a database. if i go to my template page, it loads the data and returns it to the browser via a simple php echo ... not rocket science.

now, i've written an html file using jquery and an ajax call. i load the html file in my browser and the javascript / ajax query works.

when i load the html into the database and print it out via the php / echo, the content is the exact same when i view the source, but the ajax query doesn't execute.

<html>
<head>
  <title>random query - api - get</title>
  <script src="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<pre>
executing the GET method
<script language="javascript" type="text/javascript">

$.ajax({
   type: "GET",
   url: "http://some-rand.om.api.com",
   data: "011b38b8-011f-4b03-bb21-4c5bb26600b3",
   success: function(msg){
     alert( msg );
   }
 });

</script>
</pre>
</body>
</html>

Any comments / suggestions would be great. What I find bizarre, is i have no problem copying the source of http://jquery.com/ and pasting it into the db and doing the php / echo. this works fine. maybe an onLoad() would help...hm.

sdolgy
  • 6,963
  • 3
  • 41
  • 61

3 Answers3

1

The issue is outlined in the following url:

XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

When I load the javascript console in chrome I get this:

XMLHttpRequest cannot load http://some-rand.om.api.com/user?011b38b8-011f-4b03-bb21-4c5bb26600b3. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
Community
  • 1
  • 1
sdolgy
  • 6,963
  • 3
  • 41
  • 61
  • Seems to me he was just putting a random URL as to hide the actual URL he is attempting to access. sdolgy, is the URL you are accessing on the same server as this ajax call? – Jeremy Battle Feb 13 '11 at 18:39
  • "same server". i have two vhosts configured on the same server. some-rand.om.api.com and some-rand.om.client.com ... the code is loaded in the client vhost and the api vhost is where the target javascript is pointing at. – sdolgy Feb 13 '11 at 18:41
  • What the heck? Are you answering your own question? Is that an answer or another question? Make sure everything is the same origin. some-rand.om.client.com and some-rand.om.api.com are not the same orgin. – mwilcox Feb 13 '11 at 20:48
0

To resolve the problem, on the api vhost, in the code that serves the content accessed by the ajax query, i added this (it is also php):

header('Access-Control-Allow-Origin: http://some-rand.om.client.com');

Now when I access it, it's all good and loads as expected. So, infact, it wasn't related to it being stored in the database, php or javascript. It was related to cross site scripting.

Or, to be super lazy (probably not a great idea...):

header('Access-Control-Allow-Origin: ' . $_SERVER["HTTP_ORIGIN"]);
sdolgy
  • 6,963
  • 3
  • 41
  • 61
0

If in your description of the problem you are listing the whole of the code, then the JavaScript should be wrapped in a $(document).ready(function(){ ... });.

i.e.

$(document).ready(function(){

    $.ajax({
          type: "GET",
           url: "http://some-rand.om.api.com",
          data: "011b38b8-011f-4b03-bb21-4c5bb26600b3",
       success: function(msg){alert( msg );}
   });

});
user229044
  • 232,980
  • 40
  • 330
  • 338
Neil
  • 3,229
  • 1
  • 17
  • 15