使用jQuery AJAX与总asp.net web服务去错误:不是成功: [英] using jQuery AJAX with asp.net webservices always goes to error: instead of success:

查看:116
本文介绍了使用jQuery AJAX与总asp.net web服务去错误:不是成功:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我有jQuery的code的aspx页面超过发送一个Ajax请求的ASMX Web服务文件(在同一网站上)。这回来的反应并不一致,但是,它始终触发,而不是成功回拨错误jQuery的回调。状态code 200之间变化不一致,12030和12031.在的responseText 回调不一致[空白]与实际XML之间变化的消息的JSON web服务的回报。我调试的code和web服务并没有任何异常,实际执行。

ASPX code

// code不再赘述。

 <脚本类型=文/ JavaScript的>
jQuery的(文件)。就绪(函数()
{
  jQuery.ajax({
  键入:POST,
    的contentType:应用/ JSON的;字符集= UTF-8,
    网址:CallDequeue.asmx /出列,
    数据:{},
    数据类型:JSON
    成功:函数(MSG)
    {
      警报('成功:'+ Msg.responseText);
    },
    错误:函数(MSG)
    {
      警报('失败:'+ Msg.status +:+ Msg.responseText);
    }
  });
});
< / SCRIPT>

// code ommitted为了简洁

Web服务code

  [WebService的空间(namespace =htt​​p://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)
[ScriptService]
公共类CallDequeue:System.Web.Services.WebService
{
  [的WebMethod]
  公共字符串出列()
  {
    返回{\\D \\:{\\名字\\:\\Keivan \\}};
  }
}


解决方案

在标记服务作为ScriptService,它会自动处理JSON序列化。你不应该手动序列化的响应。

如果您想退货回来的名字,那么你可以使用一个DTO类控制的语法。刚返回一个字符串,它会回来为{'D':'Keivan'},而不是{'D':{'姓':'Keivan'}}

  [ScriptService]
公共类CallDequeue:System.Web.Services.WebService
{
  公共类PersonDTO
  {
    公共字符串名字;
  }  [的WebMethod]
  公共PersonDTO出列()
  {
    VAR P =新PersonDTO();    p.FirstName =Keivan;    回磷;
  }
}

到调用语法的一些变化:

 的jQuery(文件)。就绪(函数(){
  jQuery.ajax({
    键入:POST,
    的contentType:应用/ JSON的;字符集= UTF-8,
    网址:CallDequeue.asmx /出列,
    数据:{},
    数据类型:JSON
    成功:函数(MSG){
      //除非你使用的是2.0,数据伤愈复出包裹
      //在.D对象。
      //
      //这只是Msg.d如果你返回一个字符串,而不是
      //将DTO的。
      警报('成功:'+ Msg.d.FirstName);
    },
    错误:函数(MSG){
      警报('失败:'+ Msg.status +:+ Msg.responseText);
    }
  });
});

您可以阅读更多ASP.NET AJAX的.D包装这里,如果你有兴趣。

更新:

使用ASP.NET 2.0,则需要安装的ASP.NET AJAX 1.0的扩展。此外,确保你的web.config配置为ASP.NET AJAX (最具体的HttpHandlers的部分)。

Issue

I have an aspx page with jQuery code to send an ajax request over to an asmx web service file (on the same website). The response that comes back is not consistent, however, it consistently fires the "error" jQuery callback as opposed to the "success" call back. The status code inconsistently varies between 200, 12030, and 12031. The responseText of the message to the callback inconsistently varies between [blank] and the actual XML that the json webservice returns. I debugged the code, and the webservice does actually execute without any exceptions.

ASPX Code

//Code omitted for brevity

<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>

//Code ommitted for brevity

Web Service Code

[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\"}}";
  }
}

解决方案

When you mark the service as a ScriptService, it automatically handles the JSON serialization. You shouldn't manually serialize the response.

If you want the return to come back as "FirstName", then you can use a DTO class to control the syntax. Just returning a string, it would come back as {'d':'Keivan'} instead of {'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;
  }
}

A few changes to the calling syntax:

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);
    }
  });
});

You can read more about ASP.NET AJAX's .d wrapper here, if you're interested.

Update:

Using ASP.NET 2.0, you need to install the ASP.NET AJAX Extensions v1.0. Additionally, make sure your web.config is configured for ASP.NET AJAX (most specifically the HttpHandlers section).

这篇关于使用jQuery AJAX与总asp.net web服务去错误:不是成功:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆