weixin_33674976 2012-04-27 08:11 采纳率: 0%
浏览 55

修剪AJAX的PHP回声

The problem i have is with a simple ajax mail script.

I have it working fine like this:

php snippet:

if(mail($to,$subject,$message,$headers)){
echo "success";
}

And back in the ajax code its handled here :

         if($.trim(data) == "success"){
           // do the success stuff
         }else{
           // doo the fail stuff
         }

My concern is that in the jquery code "trim" is not supported in all versions of explorer. So i would rather have the php return the string "success" already trimmed. But changing things to what you see below does not work as i expected, and runs the fail function in the ajax..?

Ammended php :

if(mail($to,$subject,$message,$headers)){
$success = trim("success");
echo $success;
}

Ammended Ajax :

     if(data == "success"){
         // do success stuff does not happen anymore
     }else{
         // now it always does fail stuff, so comparison is always false
     }

Why is doing the trimming in the php instead of the ajax letting me down please..?

  • 写回答

3条回答 默认 最新

  • 狐狸.fox 2012-04-27 08:16
    关注

    Your problem is not with the string "success". Trimming just removes spaces and white-characters (newlines, etc.) from the beginning and ending of a string. And in "success" there is no such thing, so it does nothing. However, if trimming on the JavaScript side works, then your PHP must be producing some whitespace characters, so it appears in the response. But this is not in the code you posted, there is no problem with the work "success". You have to go through your code and find the source of the newlines/spaces/white characters.

    Usually, the problem is with Unicode BOM marks (look that up and open your PHP files in an editor that can show you if they are present or not) or newlines at the end of a PHP file (remove these).

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料