View Full Version : HTML as JSON attribute via AJAX

03-13-2009, 08:56 PM
Hey all, first post.

I'm trying to using ajax to fetch a JSON object via PHP. When I construct the object in the PHP script I create an attribute called "content", and in that attribute is some html... only of course when I try to parse the JSON response on the client side, I get syntax errors.

I've stripped out all \n and \r characters and used addslashes() to escape the quotes, yet still get syntax errors. What am I missing?

Or alternatively, is there a better way to return presentation content through ajax and parse it into html on the client side?

Remarkably, google is not my friend in this instance... any help much appreciated.


EDIT: whoops, looks like I might have posted this in the wrong section. Any mod feel free to move if appropriate.

03-13-2009, 09:45 PM
OK, so I've partially solved it. A rather wierd result.

Firstly, I found that single quotes weren't being handled properly. After trying for ages myself I figured instead of using str_replace etc, I could use json_encode PHP functions. That solved the single quote problem.

Now onto a weird problem indeed. For some reason all my HTML is fine now, *except* for the tag <form>, which causes a parse error. It could just be the json javascript library I'm using has some bug, but I don't know. Strange, just that one tag that causes a parse error and no others.