繁体   English   中英

使用jQuery AJAX与asp.net webservices总是会出错:而不是成功:

[英]using jQuery AJAX with asp.net webservices always goes to error: instead of success:

问题

我有一个带有jQuery代码的aspx页面,可以将ajax请求发送到asmx web服务文件(在同一个网站上)。 返回的响应并不一致,但它始终触发“错误”jQuery回调而不是“成功”回调。 状态代码不一致地在200,12030和12031之间变化。对回调的消息的responseText在[blank]和json webservice返回的实际XML之间不一致地变化。 我调试了代码,并且webservice确实执行而没有任何异常。

ASPX代码

//为简洁起见省略了代码

<script type="text/javascript">
jQuery(document).ready(function()
{
  jQuery.ajax({
  type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "CallDequeue.asmx/Dequeue",
    data: "{}",
    dataType: "json",
    success: function(Msg)
    {
      alert('success:' + Msg.responseText);
    },
    error: function(Msg)
    {
      alert('failed:' + Msg.status + ':' + Msg.responseText);
    }
  });
});
</script>

//为简洁起见,省略了代码

网络服务代码

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class CallDequeue : System.Web.Services.WebService
{
  [WebMethod]
  public string Dequeue()
  {
    return "{\"d\":{\"FirstName\":\"Keivan\"}}";
  }
}

将服务标记为ScriptService时,它会自动处理JSON序列化。 您不应手动序列化响应。

如果您希望返回“返回”作为“FirstName”,则可以使用DTO类来控制语法。 只需返回一个字符串,它就会以{'d':'Keivan'}而不是{'d':{'FirstName':'Keivan'}}返回。

[ScriptService]
public class CallDequeue : System.Web.Services.WebService
{
  public class PersonDTO
  {
    public string FirstName;
  }

  [WebMethod]
  public PersonDTO Dequeue()
  {
    var p = new PersonDTO();

    p.FirstName = "Keivan";

    return p;
  }
}

调用语法的一些更改:

jQuery(document).ready(function() {
  jQuery.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "CallDequeue.asmx/Dequeue",
    data: "{}",
    dataType: "json",
    success: function(Msg) {
      // Unless you're using 2.0, the data comes back wrapped
      //  in a .d object.
      //
      // This would just be Msg.d if you return a string instead
      //  of the DTO.
      alert('success:' + Msg.d.FirstName);
    },
    error: function(Msg) {
      alert('failed:' + Msg.status + ':' + Msg.responseText);
    }
  });
});

如果您有兴趣,可以在这里阅读有关ASP.NET AJAX的.d包装器的更多信息

更新:

使用ASP.NET 2.0,您需要安装ASP.NET AJAX Extensions v1.0 此外,请确保为ASP.NET AJAX (最具体地说是HttpHandlers部分) 配置了web.config

这个问题很可能会对你有所帮助。

否则,我将此Web服务转换为页面方法,它立即起作用。 你有那个选择吗?

CS:

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static string Dequeue()
    {
        return "{\"d\":{\"FirstName\":\"Keivan\"}}";
    }
}

ASPX:

<script type="text/javascript">
    jQuery(document).ready(function()
    {
            jQuery.ajax({
            type: "POST",
              contentType: "application/json; charset=utf-8",
              url: "test.aspx/Dequeue",
              data: "{}",
              dataType: "json",
              success: function(Msg)
              {
                    alert('success:' + Msg.responseText);
              },
              error: function(Msg)
              {
                    alert('failed:' + Msg.status + ':' + Msg.responseText);
              }
            });     
    });

有关更多信息,请查看文章和其他Encosia文章。

你说它是json,但它返回xml。 我看到那里有轻微的脱节。

这么简单的答案。 我的web.config没有启用ajax,因此所有调用(无论我的webservice是一个脚本服务)都返回XML而不是纯json。

尝试使用[ScriptMethod]标记您的Web方法

如:

[WebMethod]
[ScriptMethod]

我要感谢所有响应者。

请务必在要使用的方法前添加这些属性

[WebMethod] 
[ScriptMethod] 

不确定何时需要ScriptMethod?

奇怪的是,他的下载代码中没有[Script Service][WebMethod]

无论如何,在上述变化之后,aJax 500 12030 12031错误消失了。

暂无
暂无

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

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