admin管理员组

文章数量:1406968

I trying to display the content of a MySQL TEXT field inside a <pre></pre> html markup.
To acplish that I use an ajax call

$.ajax({
    type : 'POST',
    url : webserviceFile,
    data : {
        action : "confFile",
        id : id
    },
    success : function(data) {
        $("#configFile").html(data);
    },
    error : function(e, f, g) {
        return ("An error occure: " + e + "\n" + f + "\n" + g);
    }
});

On server Side I have that piece of php :

switch($action) {   
    case 'confFile' :
            $value = getConfigurationFile($_POST['id']);
            echo "##$value##";
    break;
}

Finally my orignal html file is :

<body>
    <pre id="configFile" ondblclick='selectText( "configFile" )'></pre>
</body>

So the script does work, now my problem is that on Firefox IE8 and Chromium I have 3 spaces in front of my ajax result. For example, one of the file contains :
no config file + the two dashes I add to be sure that it does not e from my php script, in my <pre> mark I will end up with :

<pre id="configFile" ondblclick="selectText( &quot;configFile&quot; )" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 67px; max-height: 667px; height: auto;">
   ##no config##</pre>

And as you can see there's 3 spaces in front of my ##no config## string.

I'm using the last stable jQuery and watching what the POST return with Firebug I do see those spaces, I remove every echo or print in my php code except the one returning the ##no config## but still those spaces appear.

Do you have any idea on how and why those fµµµµ appears, and do you know a work around for that ?

Thanks

Edit 1: Using trim is one solution but the content of the MySQL field is not modifiable and I can't remove white space at the end or at the beginning of that content for displaying.

Complete answer:
Remove every spaces before and after the php markup <?php and ?> in all you php files to avoid that behaviour that should do the trick

I trying to display the content of a MySQL TEXT field inside a <pre></pre> html markup.
To acplish that I use an ajax call

$.ajax({
    type : 'POST',
    url : webserviceFile,
    data : {
        action : "confFile",
        id : id
    },
    success : function(data) {
        $("#configFile").html(data);
    },
    error : function(e, f, g) {
        return ("An error occure: " + e + "\n" + f + "\n" + g);
    }
});

On server Side I have that piece of php :

switch($action) {   
    case 'confFile' :
            $value = getConfigurationFile($_POST['id']);
            echo "##$value##";
    break;
}

Finally my orignal html file is :

<body>
    <pre id="configFile" ondblclick='selectText( "configFile" )'></pre>
</body>

So the script does work, now my problem is that on Firefox IE8 and Chromium I have 3 spaces in front of my ajax result. For example, one of the file contains :
no config file + the two dashes I add to be sure that it does not e from my php script, in my <pre> mark I will end up with :

<pre id="configFile" ondblclick="selectText( &quot;configFile&quot; )" class="ui-dialog-content ui-widget-content" style="width: auto; min-height: 67px; max-height: 667px; height: auto;">
   ##no config##</pre>

And as you can see there's 3 spaces in front of my ##no config## string.

I'm using the last stable jQuery and watching what the POST return with Firebug I do see those spaces, I remove every echo or print in my php code except the one returning the ##no config## but still those spaces appear.

Do you have any idea on how and why those fµµµµ appears, and do you know a work around for that ?

Thanks

Edit 1: Using trim is one solution but the content of the MySQL field is not modifiable and I can't remove white space at the end or at the beginning of that content for displaying.

Complete answer:
Remove every spaces before and after the php markup <?php and ?> in all you php files to avoid that behaviour that should do the trick

Share Improve this question edited Dec 18, 2013 at 9:42 Kiwy asked Dec 18, 2013 at 8:52 KiwyKiwy 3463 gold badges12 silver badges48 bronze badges 1
  • use trim() to remove extra whitespaces – Nishu Tayal Commented Dec 18, 2013 at 8:55
Add a ment  | 

2 Answers 2

Reset to default 7

make sure that your server side php file does not have any spaces before the php open tags and if possible do not use '?>'-tags to prevent trailing spaces aswell

try jQuery.trim() or replace()

var newAnswer = data.replace(" ", "");
$("#configFile").html(newAnswer );

本文标签: javascriptAjax call or php script add spaces to my data resultStack Overflow