+ -
当前位置:首页 → 问答吧 → jquery ajax返回包含"\0"的"text"类型,IE下截断,FF chrome正常

jquery ajax返回包含"\0"的"text"类型,IE下截断,FF chrome正常

时间:2011-11-28

来源:互联网


代码如下

$.ajax({
  type:"POST",
  datetype:"text",
  url:"/ashx/show.ashx",
  success:function(msg){  
  alert(msg);
  }
  });

show.ashx输出“Hello \0 World”,FF,chrome获取正常,IE只能获取“Hello",被“\0”截断,只考虑jquery的话,怎么兼容IE?

作者: pythen   发布时间: 2011-11-28

我这边测试没有问题,你的服务器端代码帖出来吧 另外dataType 大写的T

作者: ifandui   发布时间: 2011-11-28

[WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  public class show : IHttpHandler
  {

  public void ProcessRequest(HttpContext context)
  {
  context.Response.ContentType = "text/plain";
  context.Response.Write("Hello \0 World");
  }

  public bool IsReusable
  {
  get
  {
  return false;
  }
  }
  }

作者: pythen   发布时间: 2011-11-28

context.Response.ContentType = "text/plain";
试试text/html

作者: ifandui   发布时间: 2011-11-28

还是不行

作者: pythen   发布时间: 2011-11-28

你是想要达到什么效果???
context.Response.Write("Hello \\0 World");

这样能显示\0这个字符本身

作者: p2227   发布时间: 2011-11-28

"Hello \\0 World"

作者: ifandui   发布时间: 2011-11-28

实际上"\0”这个是预期之外的字符,在不改动后台代码的情况下,在FF,chrome下可以处理掉,而在IE下却被"\0"截断,无法拿到原来的全部内容,如果要改动后台就很简单。顺带讨论一下出现截断这种情况的原因

作者: pythen   发布时间: 2011-11-28

HTML code
<script>
    $.ajax({
        type: "POST",
        datetype: "text",
        url: "/ashx/show.ashx",
        success: function (msg) {
            alert(msg.replace(/\\0/,""));
        }
    });
</script>

作者: p2227   发布时间: 2011-11-28

msg.replace(/\\0/,"")

这个时候,在IE下msg已经被截断了

作者: pythen   发布时间: 2011-11-28

引用 9 楼 pythen 的回复:

msg.replace(/\\0/,"")

这个时候,在IE下msg已经被截断了
我的IE8没被截断,alert时候是截了

作者: p2227   发布时间: 2011-11-28

把你的后台代码改成:
  public void ProcessRequest(HttpContext context)
  {
  context.Response.ContentType = "text/plain";
  context.Response.Write("Hello \\0 World");
  }

  public bool IsReusable
  {
  get
  {
  return false;
  }
  }
或者
  public void ProcessRequest(HttpContext context)
  {
  context.Response.ContentType = "text/plain";
  context.Response.Write(@"Hello \0 World");
  }

  public bool IsReusable
  {
  get
  {
  return false;
  }
  }


在重新编译你的项目试试看。。。

作者: IT_Kid   发布时间: 2011-11-28

断点查看,success: function (msg)

这个时候msg已经只剩下"hello"

作者: pythen   发布时间: 2011-11-28

相关阅读 更多

热门下载

更多