scar
scar

Reputation: 165

Load html from js with ajax

I'm having Problems with my AJAX call. I want to append html to an existing div:

var teaser = $(this);
var url = "http://..."

ajaxHandler.send({
  url: "//www...."
  dataFilter: function (data, type) {
    teaser.append($(data))
  }
});

The problem ist, that the URL (var url) I am sending gives a JS response like this:

document.open();
document.writeln('<div id=\"mydiv\" style=\"width:190px; height:160px\">');
document.writeln('   <img src=\"http:\/\/www.abc.gif\" width=\"190\" height=\"160\" alt=\"\" border=\"0\">');
document.writeln('<\/div>');
document.close();

I get the following error:

Uncaught Error: Syntax error, unrecognized expression: document.open();

I'm using jQuery 2.x

Upvotes: 1

Views: 79

Answers (2)

low_rents
low_rents

Reputation: 4481

with an ajax request you are always getting the whole content of your request-url back (which probably also contains javascript) just like your server sends it. it doesn't execute javascript code - it just reads the sourcecode like it is returned.

why not just put pure html into your file:

<div id="mydiv" style="">
    <img src="http://www.example.com/my.gif">
</div>

then you should be fine.

Upvotes: 2

samad montazeri
samad montazeri

Reputation: 1273

If you want to get a script from server and then execute it, you should use jquery getScript.

you can also use load function like this:

$('#ad-container').load('script/location/on_server.php');

and return something like this from server(i.g make php to generate this code):

    <div id="mydiv" style="width:190px; height:160px">
        <img src="http://www.abc.gif" width="190" height="160" alt="" border="0">
    </div>

Upvotes: 0

Related Questions