【问题标题】:AJAX Request ErrorAJAX 请求错误
【发布时间】:2014-02-02 16:00:24
【问题描述】:

我在提交 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

原始:

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

而不是成功

想法?

【问题讨论】:

  • 你在 Fiddler 中调试过流量吗?
  • fail 函数应该接收几个参数,您可以在其中检查实际的错误消息。在这里检查:api.jquery.com/jquery.ajax 我猜,因为您使用的是 WebForms,所以在添加 json 之前,响应中仍有一些内容,这就是它失败的原因。
  • 我收到 requestparser 错误消息
  • 由于某种原因,您将两个 json 字符串写入响应中。{"ResponseType":0}{"ResponseType":1} 不是有效的 json
  • 嗯,所以可能 C# 代码然后.. 会玩

标签: c# javascript jquery ajax


【解决方案1】:

我刚刚使用JavascriptSerializer 类对枚举值进行了序列化测试,我收到了一个仅包含枚举值的整数表示形式的字符串(在我的例子中为3)。我认为要获得有效的 json 字符串,您需要有一个对象。试试这样的:

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

然后在javascript中:

data.ResponseType

【讨论】:

  • 请编辑您的问题并包含来自服务器的实际原始响应。您可以使用 Fiddler 程序来获得它。
  • @CorbinSpicer 再想一想,既然请求是成功的,那么只看浏览器开发工具的网络选项卡上的响应会更容易。
  • data.ResponseType 以未定义的形式返回
【解决方案2】:

更改您的失败方法以接受 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 方法如何解析数据。您没有提供数据类型,因此它试图猜测您返回的数据类型是什么。

【讨论】:

  • 这让我觉得返回的数据不是它所期望的格式,json 解析为 xml,xml 为 json 等等......
  • 添加了指向我的答案的链接,指向与 requestparseerror 类似的问题,即使响应成功
  • 我已将代码更改为完成而不是完成。我现在收到了我期望的正确消息和 requestresponseerror 消息
  • 尝试将 dataType: 'json' 添加到您的 ajax 构造函数中,我认为 ajax 方法“智能猜测”在这种情况下并不那么智能,并且不知道您是什么数据类型返回。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多