简体   繁体   English

Ajax调用或php脚本为我的数据结果添加空格

[英]Ajax call or php script add spaces to my data result

I trying to display the content of a MySQL TEXT field inside a <pre></pre> html markup. 我试图在<pre></pre> html标记内显示MySQL TEXT字段的内容。
To accomplish that I use an ajax call 为了实现这一点,我使用ajax调用

$.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 : 在服务器端我有这段PHP:

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

Finally my orignal html file is : 最后我的orignal html文件是:

<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. 所以脚本确实有效,现在我的问题是在Firefox IE8和Chromium上我的ajax结果前面有3个空格。 For example, one of the file contains : 例如,其中一个文件包含:
no config file + the two dashes I add to be sure that it does not come from my php script, in my <pre> mark I will end up with : no config file +我添加的两个短划线以确保它不是来自我的PHP脚本,在我的<pre>标记中我将最终得到:

<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. 正如你可以看到我的##no config## string前面有3个空格。

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. 我正在使用最后一个稳定的jQuery并观察POST返回Firebug我看到的那些空格,我删除了每个echo或打印在我的PHP代码中,除了返回##no config##但仍然出现这些空格。

Do you have any idea on how and why those fµµµµ appears, and do you know a work around for that ? 你对fμμμμ的出现方式和原因有什么看法,你知道一个解决方法吗?

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. 编辑1 :使用trim是一种解决方案,但MySQL字段的内容不可修改,我无法在该内容的末尾或开头删除空格以进行显示。

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 删除所有php文件中php标记<?php?>之前和之后的每个空格,以避免应该执行该操作的行为

确保你的服务器端php文件在php打开标签之前没有任何空格,如果可能的话,不要使用'?>' - 标签来防止尾随空格

try jQuery.trim() or replace() 尝试jQuery.trim()或replace()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM