【问题标题】:Calling simple AJAX WebMethod always results in "fail" callback调用简单的 AJAX WebMethod 总是导致“失败”回调
【发布时间】:2014-06-12 06:01:53
【问题描述】:

下面的代码是我的服务器端代码。 (C#)

            [WebMethod]
            public static string InsertCV(string edu)
            {             
                return "";
            }

下面的代码是我的客户端代码。 (ASP.NET)

           var edu = {"education":[{"university":"111","speciality":"222","faculty":"333","degree":"444","start":"555","end":"666"}]}

            var post = $.ajax(
             {
                 type: "POST",
                 data: edu,
                 url: "Add_CV.aspx/InsertCV",
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 async: true,
                 cache: false
             })
            post.done(function (data, teStatus, jqXHR) {
                if (data.d == "")
                { alert("ok"); }
            });
            post.fail(function (jqXHR, e) {
                alert("error");
            });

我想用 ajax post 方法将用户的数据发送到服务器。但每次post.fail() 函数执行。 请帮助我,我的错误在哪里。 可能在服务器端 InsertCV(string edu)string 不适合这种情况。我不知道。

【问题讨论】:

  • 对不起,是我的错
  • 可能是您以错误的方式传递数据。只需在 AJAX 调用的数据对象中尝试 JSON.stringify(edu) 即可。谢谢
  • @user3624946 使用浏览器开发者工具检查请求和服务器响应——即它是 403 吗? 404? 5xx?另外,尝试将调试器附加到 ASP.NET 代码,看看是否有任何异常。
  • @Arpit Jain,我试过了,但结果是一样的。
  • @user2864740 ,我尝试并得到错误 500。

标签: c# jquery ajax json webmethod


【解决方案1】:

这个:

public static string InsertCV(string edu)

需要一个名为 edu 的参数,其类型为 string。您的 AJAX 调用是传入一个未命名的 JavaScript 对象,它是 not 字符串。试图解析请求的框架代码根本不会匹配您的InsertCV 方法,并最终放弃500 - Internal Server Error 结果。

要将这样的复杂结构传递给WebMethod,您需要定义一个兼容的.NET 类来反序列化。例如:

// outer type for the parameter
public class EduType
{
    public Education[] education;

    // inner type for the 'education' array
    public class Education
    {
        public string university;
        public string speciality;
        public string faculty;
        public string degree;
        public string start;
        public string end;
    }
}

[WebMethod]
public static string InsertCV(EduType edu)
{
    return edu == null ? "null" : string.Format("{0} Items", edu.education.Length);
}

如果 JSON 字符串将反序列化为这种格式,那么应该调用这个方法。

【讨论】:

  • 我会试试你的答案并写出结果。
  • 这对我很有帮助。谢谢。我需要 10 个声望来分享我对自己问题的回答。我只使用了Education classInsertCV(Education[] education)。再次感谢。
【解决方案2】:

我找到了解决您问题的方法。

代码如下:

作为cs页面:

[WebMethod]
public static string InsertCV(object education)
{
   return "";
}

以及调用此方法:

var edu = { "education": [{ "university": "111", "speciality": "222", "faculty": "333", "degree": "444", "start": "555", "end": "666"}] }

   var post = $.ajax(
   {
      type: "POST",
      data:  JSON.stringify(edu),
      url: "ServiceTest.aspx/InsertCV",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      async: true,
      cache: false
    })
    post.done(function (data, teStatus, jqXHR) {
        if (data.d == "")
        { 
          alert("ok"); 
        }
   });
   post.fail(function (jqXHR, e) {
        alert("error");
   });

如果对你有帮助,请标记为正确。

谢谢。

【讨论】:

  • 嗨@OlafDietsche,主要有两个变化.. 1.将“对象”而不是“字符串”带入WebHTTP方法和2.在JSON.Stringify方法中从AJAX传递数据。可能这对你有帮助......谢谢。
【解决方案3】:

正如@user2864740 所问,您的方法 InsertCV 没有做任何事情。我建议首先通过在 InsertCV 方法中设置断点来测试您的 web 方法,以查看在浏览时是否会遇到此问题。其次,从您的 AJAX post 方法发送一个 JSON 数组,但在 InsertCV 方法中,您只需要一个字符串。这必须匹配。当您可以从 AJAX 调用触发 WebMethod 时,继续添加处理以写入后端数据库

【讨论】:

  • 我试图用断点检查,但好像程序没有看到断点。在我看来,数据不会出现在服务器端。可能是json类型不正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-29
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多