简体   繁体   English

jQuery / PHP-当由jQuery .ajax读取时,具有多个PHP块会产生额外的输出字符

[英]jQuery/PHP - Having more than one PHP block creates extra character of output when read by jQuery .ajax

I've managed to boil this problem down to the bare essentials: So I've got two simple .php files: 我设法将这个问题归结为基本要点:因此,我有两个简单的.php文件:

TEST.PHP test.php的

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>My Page</title>

<script src='/root/js/jquery-1.6.3.js'></script>
<script>

$(document).ready(function() {

$.ajax({

        url : 'test_ajax.php',
        type : 'GET',
        timeout : 10000,
        dataType : 'text',
        data : { 'param' : 'whatever' },
        success : function(data,status,jqXHR) {
            $('#status').html(data.length+"-"+data);
        },
        error : function(jqXHR,textStatus,errorThrown) {
            $('#status').html("Error: "+textStatus+" , "+errorThrown);
        },
        complete : function(jqXHR,textStatus) {
        }

    });

}); // end ready

</script>

</head>

<body>
    <p id='status'>
    </p>
</body>
</html>

and TEST_AJAX.PHP 和TEST_AJAX.PHP

<?php
?>

<?php
echo "ok";
?>

The data that should be returned from TEST_AJAX.PHP is "ok". 应该从TEST_AJAX.PHP返回的数据为“ ok”。 However, what is being retrieved by the jQuery/ajax code is a THREE character string which is outputted as " ok" (although the character at [0] is not equal to " "). 但是,由jQuery / ajax代码检索的是三个字符串,输出为“ ok”(尽管[0]处的字符不等于“”)。

This ONLY happens if I have the two php blocks in TEST_AJAX. 仅当我在TEST_AJAX中有两个php块时才会发生这种情况。 If I delete the first block, leaving only the second one, then it returns "ok" as a two character string, as it should. 如果删除第一个块,仅保留第二个块,那么它将按应有的那样以两个字符串的形式返回“ ok”。

What on earth is going on here? 这到底是怎么回事? AFAIK, it should be perfectly acceptable to have multiple php blocks in a .php file - even though it's obviously unnecessary in this simplified example. AFAIK,.php文件中包含多个php块应该是完全可以接受的-尽管在此简化示例中显然没有必要。

Note that there is a blank line between the two php blocks. 请注意,两个php块之间有一个空白行。 It also get's displayed. 它也得到显示。 Change it to 更改为

<?php
?><?php
echo "ok";
?>

and it should be fine. 而且应该没问题。

I tried your code in my WAMP. 我在WAMP中尝试了您的代码。 You should remove spaces between PHP blocks: 您应该删除PHP块之间的空格:

<?php
?><?php
echo "ok";
?>

And more, You can use formatted results from server-side to client-side. 而且,您可以使用从服务器端到客户端的格式化结果。 Something like JSON , For example: 类似于JSON ,例如:

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>My Page</title>

<script src='jquery.js'></script>
<script>

$(document).ready(function() {

$.ajax({

        url : 'test_ajax.php',
        type : 'post',
        timeout : 10000,
        data : { 'param' : 'whatever' },
        dataType: 'json',
        success : function(data) {
            alert(data.status);
        }
    });

}); // end ready

</script>

</head>

And in PHP: 在PHP中:

<?php
?>

<?php
    echo json_encode(array("status"=>"ok"));
?>

As you can see in this approach, no matter how many spaces are between blocks. 如您所见,无论块之间有多少空格。

White space in the PHP blocks is ignored, but the space between the PHP blocks will always be returned. PHP块中的空白将被忽略,但将始终返回PHP块之间的空白。 You may have better luck printing a json string like: 您可能会更好地打印json字符串,例如:

{'response':'ok'}

Then change your data type to json in your ajax request, and accessing the response with data.response 然后在ajax请求中将数据类型更改为json,并使用data.response访问响应

That way any extra spaces will not affect parsing 这样,任何多余的空格都不会影响解析

PHP is a templating language. PHP是一种模板语言。 Everything outside of your tags will be not parsed and returned literally. 标签之外的所有内容都不会被解析并按字面值返回。

Example

<html>
..
    <body>
        <?php echo "Hello world"; 

// white space within the tags

?>
    </body>
</html>

Will return 将返回

<html>
..
    <body>
        Hello world
    </body>
</html>

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

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