I’m doing a jQuery Ajax call to a PHP script that returns this string variable in Javascript:
"<div id=\"page\">\r\n\r\n\t"
I’m probably missing something simple here, but is there a way to revert \r\n to <br/>, but more importantly, reverting \"page\" to "page" ? I just can’t figure it out.
Javascript looks like this:
$.ajax({
type: 'POST',
dataType: "JSON",
url: sameURLasPHPscript,
success:function(data){
// successful request; do something with the data
$('body').text(data);
},
error:function(){
// failed request; give feedback to user
alert('ErrorMessage');
}
});
My php file looks like this:
$url = "someURL/controller/action/getID";
$a = file_get_contents($url);
echo json_encode($a);
Edit : Solution can be found here: http://blog.dkferguson.com/index.cfm/2011/3/15/jQuery-Mobile-styling-loaded-content
Your server is sending JSON encoded data, you should treat it likewise. Change the parameters of your jQuery.ajax so that it expects JSON data. jQuery will then parse the data and deal with all
\",\rand\n.Edit
Compare the output of these two jQuery functions:
You need to use
jQuery.htmlfunction to display HTML as HTML.