为什么PHP返回带有在ajax成功函数中回显的字符串的字符

时间:2016-06-05 21:03:08

标签: php ajax

以前,我在Windows 7上的IIS 7.5上使用PHP 5.6.3和sqlsrv,PHP在ajax成功的情况下运行良好。我升级到Windows 10,当我尝试回显一个字符串时,发现一个字符附加到ajax函数中的字符串。我想知道是什么造成的。

PHP

    $params=array($_POST['uID']);
    $sql="SELECT * FROM users WHERE uID=?";
    $stmt=sqlsrv_query($conn,$sql,$params);
    if(!$stmt)
    {
      echo "1"; 
    }
    else
    {
        $row=sqlsrv_fetch_array($stmt);
        if($row>0)
        {
           echo "1";
        }
        else
        {
           echo "0";
        }
    }

AJAX

    var uID=$('#fUID').val();
    $.ajax(
    {
        type:"POST",
        url:"adminDB.php",
        data:"uID="+uID+"&action="+action,
        cache:true,
        success:function(msg)
        {
            alert(msg);
        }
    });

输出:

enter image description here

3 个答案:

答案 0 :(得分:1)

我的猜测是,您的include_recipe 'deploy' application = search(:aws_opsworks_app).first deploy = node[:deploy][application['shortname']] directory deploy[:deploy_to] do group deploy[:group] owner deploy[:user] mode "0775" action :create recursive true end prepare_git_checkouts( user: deploy[:user], group: deploy[:group], home: deploy[:home], ssh_key: "#{application['app_source']['ssh_key']}" ) ruby_block "change HOME to #{deploy[:home]} for source checkout" do block do ENV['HOME'] = deploy[:home] end end deploy application['shortname'] do before_migrate do link_tempfiles_to_current_release # Do NPM Install here. end branch application['app_source']['revision'] create_dirs_before_symlink deploy[:create_dirs_before_symlink] deploy_to deploy[:deploy_to] # defaults to 'name' if not specified environment OpsWorks::Escape.escape_double_quotes(application[:environment]) group deploy[:group] keep_releases deploy[:keep_releases] migrate false provider Chef::Provider::Deploy.const_get(deploy[:chef_provider]) purge_before_symlink(deploy[:purge_before_symlink]) unless deploy[:purge_before_symlink].nil? repository application['app_source']['url'] revision application['app_source']['revision'] scm_provider Chef::Provider::Git shallow_clone deploy[:shallow_clone] symlinks(deploy[:symlinks]) unless deploy[:symlinks].nil? user deploy[:user] action deploy[:action] # defaults to :create if not specified rollback_on_error true end ruby_block "change HOME back to /root after source checkout" do block do ENV['HOME'] = "/root" end end 文件在文件开头包含BOM 如果您要从PHP文件中删除它(您可以使用notepad++和其他一些文本编辑器执行此操作),您将能够按照自己喜欢的方式比较字符串。

答案 1 :(得分:0)

尝试设置标头以输出json,因为这是jquery的ajax函数默认所期望的:

header('Content-Type: application/json; charset=UTF-8');

您应该在echo声明之前执行此操作。

答案 2 :(得分:0)

在想知道我对这个问题的看法后......我终于找到了办法。我使用了json_encode()并能够比较返回的字符串。我肯定知道问题是Windows 10 32位中PHP和SQLSRV驱动程序之间的问题。

$resp=array('message'=>'1');
echo json_encode($resp);