douzhong4222 2011-09-05 23:28
浏览 114
已采纳

为什么返回会添加换行符?

I'm calling saveVideo.php via ajax and $result always has two linebreaks at the top, I don't see where they could be coming from. It should be simply the $id variable in addVideo. I need the id to ajax in the new video once it's uploaded.

Even with trim(), the output from the saveVideo ajax call in Developer tools has 2 linebreaks in front of it.

Here's all the relevant code:

saveVideo.php:

<?php
require_once '../model.php';

$m = new Model;
$video = new Video;

$user = requireLogin();


$video->file = $_POST['url'];
$video->lib = 0; //sets library to be personal.
$video->title = mysql_escape_string($_POST['video-title']);
$video->desc = mysql_escape_string($_POST['video-description']);
$video->userId = $user->id;

$result = $m->addVideo($video);

echo trim($result);
?>

the addVideo function:

function addVideo($video, $lib = 1) {
        // Adds video to database and associates it with a user id

        $qstring = "mysqlquery";

        $result = mysql_query($qstring);

        $id = mysql_insert_id();

        if (!$result) {
            die("Error adding video to database.");
        }
        return $id;
    }
  • 写回答

2条回答 默认 最新

  • dpstir0081 2011-09-05 23:38
    关注

    Look for unnecessary blank lines before your opening <?php or after your closing ?> tags. If there are blank lines outside of a <?php ?> block they will be treated as part of the page content and be sent as part of the PHP response.

    This applies to ../model.php as well. Blank lines in there will do the same thing.


    Also, when you're debugging something like this you'll save a lot of hair pulling if you use var_dump rather than echo.

    var_dump($result);
    

    This would produce output like

    string(2) "14"
    

    which would tell you that $result is not the source of the mysterious line breaks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流