user790843
user790843

Reputation: 133

Can't find error in code about retrieve contents from HTML file

I would preciate if someone could help me to get the code below to work. The goal is to read the content of a web page and display it in a div that is called #content. What could be wrong?

function content(){
 $.ajax({
  url : “page.html”,
  success : function (data) {
   $(“#content”).html(data);
  }
 });
}

Upvotes: 1

Views: 100

Answers (2)

andyb
andyb

Reputation: 43823

The page that includes the script must be loaded from a web server otherwise you will be seeing XMLHttpRequest cannot load xxx.html. Origin null is not allowed by Access-Control-Allow-Origin style errors. Cross-site requests like this are not allowed (be default) due to security concerns, although they are possible.

If you serve your test page from a web server, the request will be allowed. Most modern browsers include a JavaScript or Error console that detail errors of this kind.

Upvotes: 0

Niklas
Niklas

Reputation: 30012

Your quotation marks certainly look quite odd.

Change them to normal ":

function content(){
 $.ajax({
  url : "page.html",
  success : function (data) {
   $("#content").html(data);
  }
 });
}

You could just use .load() as well though...

$('#content').load("page.html");

Upvotes: 2

Related Questions