0

I am a newbie to jQuery and was trying to load content dynamically using AJAX and jQuery using the following code but its not working.

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("button").click(function()
{
$(".text").load("http://api.jquery.com/get/");

});

});
</script>
<title>AJAX with jQuery</title>
</head>
<body>
<div class="text">Sushil</div>
<button>Load Text</button>
</body>
</html>
kaysush
  • 4,797
  • 3
  • 27
  • 47

2 Answers2

2

You are violating the same origin policy. Just open your console and see the error message when you click the button. Things will become clearer to you. If not please get back

Rasmus
  • 8,248
  • 12
  • 48
  • 72
  • I tried to load a local html file in the same directory bu using $(".text").load("txtfile.txt") but still no results. Can you give me an example how to load a file in div using AJAX? – kaysush Jul 13 '12 at 08:32
  • your code is working perfectly for me if both files are in the same directory.. can you tell me what error you are getting... just open the console you will get to know the error – Rasmus Jul 13 '12 at 08:39
  • hmm tough call.. ok what browser are you using – Rasmus Jul 13 '12 at 08:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13834/discussion-between-alice-and-sushil-kumar) – Rasmus Jul 13 '12 at 08:44
0

I ran your code and I got this error: XMLHttpRequest cannot load http://api.jquery.com/get/. Origin null is not allowed by Access-Control-Allow-Origin.

I googled it and I got this: Origin null is not allowed by Access-Control-Allow-Origin

Community
  • 1
  • 1
Armando
  • 459
  • 1
  • 8
  • 22