1

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>

4 Comments

because some images (adserver stuff) should only be loaded if the user clicks on a certain button - unfortunatly
what is the problem with that? you can do: $('#that-button').click(function(){ $('#ad-container').load('script/location/on_server.php'); });
OMG! i misunderstood your question. didn't i? can you control your server response or not? can you change the way server sends you information?
I guess so. No, I cannot control the sever response or change the way the server sends the information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.