AJAX请求错误

时间:2014-01-14 14:34:16

标签: c# javascript jquery ajax

我在提交ajax请求时遇到错误。

这是jQuery / ajax

  <script type="text/javascript">
      $(function () {
          $('#BookButton').click(function (event) {
              var form = $('#Form1');
              $.ajax({
                  type: form.attr('method'),
                  url: form.attr('action'),
                  data: $("#BookRoom :input").serialize()
              }).done(function (data) {
                  $('#BookRoom').modal('hide');
                  if (data === ResponseType.Success) {
                      $('#SuccessMsg').text('Meeting Booked');
                      $('#SuccessMessage').modal('show');
                  }
                  else if (data === ResponseType.Reject) {
                      $('#SuccessMsg').text('There are conflicts');
                      $('#SuccessMessage').modal('show');
                  }
                  else if (data === ResponseType.Reject) {
                      // Notify user: It is your fault
                  }
                  else {
                      $('#SuccessMsg').text('Test');
                      $('#SuccessMessage').modal('show');
                  }


                  // Optionally alert the user of success here...


                  setTimeout(function () { $('#SuccessMessage').modal('hide'); }, 3000);
              }).fail(function () {
                  // Optionally alert the user of an error here...
                  alert("Error submitting AJAX request");
              });
              event.preventDefault(); // Prevent the form from submitting via the browser.
          });
      });

C#摘要

            public enum ReponseType : int
            {
            Success = 0,
            Reject = 1

              }



            if (ITMtgfapts.Items.Count > 0) // If there is more than one item
            {


                Response.AddHeader("Content-type", "application/json");
                Response.ContentType = "application/json";
                Response.Write(JsonConvert.SerializeObject(ReponseType.Reject));
                Response.End();
            }


            else
            {

                //Success
                appointment.RequiredAttendees.Add(roomEmail);



                appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
                Response.AddHeader("Content-type", "application/json");
                Response.ContentType = "application/json";
                Response.Write(JsonConvert.SerializeObject(ReponseType.Success));
                Response.End();


            }

值得注意的是,这是一个WebForms应用程序。当我提交表格时,我得到:

  alert("Error submitting AJAX request");

错误讯息。然而,C#仍然执行(即它预订会议室)

有什么想法吗?

谢谢

编辑:

提琴手回应

JSON:

ResponseType=0

RAW:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 14 Jan 2014 14:58:54 GMT
Content-Length: 36

{"ResponseType":0}{"ResponseType":1}

编辑2:

让它正常工作,现在回复:

  HTTP/1.1 200 OK
  Cache-Control: private
  Content-Type: application/json; charset=utf-8
  Server: Microsoft-IIS/7.5
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Date: Tue, 14 Jan 2014 15:15:28 GMT
  Content-Length: 18

  {"ResponseType":0}

我从catch {}中删除了代码,因为当以JSON格式输出页面时,它会搞砸并导致它被捕获。

现在它总是返回

  else {
                  $('#SuccessMsg').text('Test');
                  $('#SuccessMessage').modal('show');
              }

而不是成功

想法?

2 个答案:

答案 0 :(得分:1)

我刚刚使用JavascriptSerializer类运行测试来序列化枚举值,我收到的字符串只包含枚举值的整数表示(在我的例子中为3)。我想要获得一个有效的json字符串,你需要有一个对象。尝试这样的事情:

JsonConvert.SerializeObject(new { ResponseType = ReponseType.Success })

然后在javascript:

data.ResponseType

答案 1 :(得分:1)

更改失败方法以接受jquery api中定义的三个参数。我猜测返回时会出现某种序列化错误。

.fail(function (jqXHR, textStatus, errorThrown ) {
  // Optionally alert the user of an error here...
  alert(textStatus);
  alert(errorThrown); // this line should give you the error happening
  alert("Error submitting AJAX request");
});

编辑1:你也可以查看这个问题,看来这家伙有类似的问题,返回成功,但仍然解雇了错误,看来是他的jQuery版本使用: jQuery returning "parsererror" for ajax request

编辑2:尝试添加dataType: 'json',作为您的ajax构造函数的选项。 json是严格解析的,但你也没有告诉ajax方法如何解析数据。您尚未提供dataType,因此它试图猜测您返回的数据类型是什么。