AJAX请求错误

科尔宾·斯派塞(Corbin Spicer)

我在提交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');
              }

而不是成功

有想法吗?

杰森p

我只是使用JavascriptSerializer该类运行了一个序列化枚举值的测试,并且我收到了一个仅包含枚举值的整数表示形式的字符串(3在我的情况下)。我认为要获取有效的json字符串,您需要有一个对象。尝试这样的事情:

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

然后在javascript中:

data.ResponseType

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

$ .ajax 400错误请求

来自分类Dev

AJAX发布请求错误

来自分类Dev

Ajax 400 错误请求

来自分类Dev

AJAX错误:400错误的请求

来自分类Dev

AJAX请求的“ 400错误请求”响应

来自分类Dev

AJAX请求跨域请求被阻止错误

来自分类Dev

AspNetMvc中的Ajax错误请求

来自分类Dev

POST 400错误,ajax请求

来自分类Dev

Spring MVC AJAX错误请求错误

来自分类Dev

AJAX POST中的400错误的请求错误

来自分类Dev

for循环中的AJAX请求返回错误的结果

来自分类Dev

Yii2 Ajax错误请求(#400)

来自分类Dev

Ajax请求中的angularjs错误处理

来自分类Dev

Spring MVC Ajax 400错误请求

来自分类Dev

Ajax请求在HandleUnauthorizedRequest之前遇到错误功能

来自分类Dev

AJAX POST请求中的HTTP错误400

来自分类Dev

发送Ajax请求时出现500错误

来自分类Dev

Drupal外部PHP Ajax请求错误

来自分类Dev

Spring MVC 400错误请求Ajax

来自分类Dev

Ajax调用Spring MVC Controler错误的请求

来自分类Dev

Javascript jQuery错误重载Ajax请求?

来自分类Dev

Flask AJAX:错误的请求。CSRF令牌丢失

来自分类Dev

Spring Ajax调用上的400错误请求

来自分类Dev

POST方法是ajax给出400(错误请求)

来自分类Dev

jQuery AJAX请求JSON上的'parseerror'错误

来自分类Dev

发送Ajax请求时出现500错误

来自分类Dev

科尔多瓦本地ajax请求错误

来自分类Dev

从Ajax到Spring MVC的请求。404错误

来自分类Dev

for循环中的AJAX请求返回错误的结果