8 Replies - 733 Views - Last Post: 16 October 2013 - 01:08 PM

#1 Deanimon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 13-December 10

Simple ajax not working

Posted 16 October 2013 - 11:04 AM

I am very new to web programming. I'm trying to load an html page into a div through $.get and $.ajax, but neither of them is working. I put an alert to see if the code is running at all. The solution should be very simple, but I can't seem to find it.

$.get:
$.get("pages/page1.html", function(response)
		{
			alert(response);
		});



$.ajax:
$.ajax({
          		url: "pages/page1.html",
          		success: function(data)
				{
					alert(data);
        		}
        });



This is between the <head> tags in index.html. Actions.js is where I put the code above.
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="actions.js"></script>



Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple ajax not working

#2 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6382
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Simple ajax not working

Posted 16 October 2013 - 11:27 AM

Are there any error messages in your browser's console? See my signature for console references.

Check the Network tab (it might be called something slightly different in different browsers) as well.

The pages folder is, of course, in the same location as the current page you are running?

What browser are you using? Try it in different browsers.
Was This Post Helpful? 0
  • +
  • -

#3 Deanimon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 13-December 10

Re: Simple ajax not working

Posted 16 October 2013 - 11:38 AM

Thanks for the quick reply.

I tested in Chrome. Chrome gives me nothing whatsoever, FireFox gives me the alert '[object XMLDocument]'.
The folder 'pages' is in the same folder as index.html.

The Chrome console outputs this in the console:
OPTIONS file:///(path to folder)/pages/page1.html Origin null is not allowed by Access-Control-Allow-Origin. jquery.min.js:4
XMLHttpRequest cannot load file:///(path to folder)/pages/page1.html. Origin null is not allowed by Access-Control-Allow-Origin.

I'm not hosting these files anywhere, I'm just running it on my computer. Could that be a problem?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6382
  • View blog
  • Posts: 25,781
  • Joined: 12-December 12

Re: Simple ajax not working

Posted 16 October 2013 - 12:00 PM

With Chrome create a shortcut to this and add the command-line argument:

--allow-file-access-from-files

to allow local file access.

I'm not sure about doing this with FireFox so try Chrome first.
Was This Post Helpful? 0
  • +
  • -

#5 Deanimon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 13-December 10

Re: Simple ajax not working

Posted 16 October 2013 - 12:14 PM

I did what you said, but I still have the same problem.
I am using Windows by the way.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,058
  • Joined: 08-June 10

Re: Simple ajax not working

Posted 16 October 2013 - 12:16 PM

alternately, run it through a HTTP server (e.g. Apache (can be found in the XAMPP package)).
Was This Post Helpful? 1
  • +
  • -

#7 Deanimon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 13-December 10

Re: Simple ajax not working

Posted 16 October 2013 - 12:50 PM

Thank you, that worked.

However, it created a problem for me. The animations that previously worked, don't work anymore.
This is what the animation looks like:

$("#colorblock").animate({backgroundColor: '#f98633'}, 500);

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,058
  • Joined: 08-June 10

Re: Simple ajax not working

Posted 16 October 2013 - 12:55 PM

is there anything in the Error Console?
Was This Post Helpful? 0
  • +
  • -

#9 Deanimon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 13
  • Joined: 13-December 10

Re: Simple ajax not working

Posted 16 October 2013 - 01:08 PM

No there's nothing in the console in Chrome.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1