0

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

4
  • What does your PHP code look like? You should probably fix it from there. Commented May 12, 2012 at 19:51
  • The fact of the matter is, some tweaking of the server-side script would be best, since it is producing this unhelpful output. Commented May 12, 2012 at 19:53
  • @David everything is as it should be, until I do json_encode($b). Is there a way to do this in PHP? Commented May 12, 2012 at 19:58
  • @SalmanA That's a leftover from testing. I will remove it now. Commented May 12, 2012 at 20:22

2 Answers 2

3

You can strip the slashes using this function: http://phpjs.org/functions/stripslashes:537

And you can replace \r\n with this function: http://phpjs.org/functions/nl2br:480

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

1 Comment

This is just a workaround, it won't solve the actual problem.
2

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 \", \r and \n.

Edit

Compare the output of these two jQuery functions:

$('body').text("<b>bold</b>"); // will write <b>bold</b> literally
$('body').html("<b>bold</b>"); // will write bold in bold font

You need to use jQuery.html function to display HTML as HTML.

8 Comments

I decided it would be best if I don't do json_encode(). Is there a way I can convert these characters in PHP instead of javascript?
Your answer had the same result as changing json_encode($b) to echo $b as I stated in my Question Edit.
Changing from .text() to .html() almost did the trick. Now firebug show the HTML-code being injected into the HTML page, but it is greyed out, and it doesn't show in browser yet either.
Not sure. By grayed out do you mean the element is displayed dimmed? Hidden elements or elements not injected in DOM appear dimmed.
I googled "jquery mobile page loses styling when loading ajax content" and ended up upon blog.dkferguson.com/index.cfm/2011/3/15/…
|

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.