【问题标题】:Json data not returning back to successJson数据没有返回成功
【发布时间】:2013-12-02 06:32:12
【问题描述】:

请考虑以下代码。我正在使用下面的 ajax 调用加载我的产品视图。

$.ajax({
      cache: true,
      type: "GET",
      url: url,
      data: json.stringify(data),
      success: function (data) {
          var mainview = $(data).find(".maindiv");
          $('.maindiv').replaceWith(mainview);
                    }
     },
      error: function (xhr, ajaxOptions, thrownError) {
          alert('Error Occured...... No Products Found');

      },
      complete: function () { subscribeProgress.hide(); }
  });       

现在,当我加载此视图时,每个产品都有一个添加到购物车按钮,在该按钮上执行另一个 ajax 调用以检查客户是否已注册或访客,并显示相应的注册弹出窗口。现在注册客户,调用了另一个ajax方法,正常工作,但是json数据没有返回成功,直接显示在页面上。

下面是弹窗注册客户的id码

$('#formpopupReg').unbind('submit');
        $('#formpopupReg').bind('submit', function () {
            if ($(this).valid()) {
                $.ajax({
                    url: this.action,
                    type: this.method,
                    // you can post your model with this line
                    data: $(this).serialize(),
                    success: function (data) {//Json should get back to this call but doesnt

                        if (data.status == "Wrong email") {
                            $('#modelerrors').text("Wrong email");
                        }

                        if (data.status == "emailexists") {
                            //Code on Error

                        }
                        if (data.status == "registersuccess") {

                            location.reload();
                        }


                    },
                    error: function (xhr, ajaxOptions, thrownError) {

                        alert('Internal Error.');
                    },

                    complete: function () { }

                });
            }
            return false;
        });

任何解决方案。我希望 json 数据返回成功调用。

【问题讨论】:

  • 尝试在success: function (data) 中查看"data" 是否为带有console.log(data) 的数组或对象。
  • 实际上当我用ajax加载页面时,通过inspect元素看不到任何jquery代码

标签: c# ajax asp.net-mvc-3 jquery


【解决方案1】:

dataType:"json" 添加到您的 ajax 调用中。

【讨论】:

  • 问题只是当我的视图是通过 ajax 调用加载时,即从第一个 ajax 调用
【解决方案2】:

试试这个,问题是您的} 在您的代码中是多余的,如果您使用json,则缺少dataType: 'json',

$.ajax({
      cache: true,
      url: url,
      dataType: 'json',
      data: json.stringify(data),
      success: function (data) {
          var mainview = $(data).find(".maindiv");
          $('.maindiv').replaceWith(mainview);
                    }     
      error: function (xhr, ajaxOptions, thrownError) {
          alert('Error Occured...... No Products Found');

      },
      complete: function () { subscribeProgress.hide(); }
  }); 

【讨论】:

  • 我本可以粘贴错误,但通话正常。尝试了您的解决方案,但没有奏效。问题仅是当我的视图通过 ajax 调用加载时,即从第一个 ajax 调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 2012-03-26
  • 1970-01-01
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多