简体   繁体   English

Json数据未恢复成功

[英]Json data not returning back to success

Please consider below code. 请考虑以下代码。 I am loading my products view using below ajax call. 我正在使用下面的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(); }
  });       

Now when I load this view there is add to cart button for each product on which another ajax call is executed to check if customer is registerd or guest and accordingly popup for register is shown. 现在,当我加载该视图时,每个产品都有一个“添加到购物车”按钮,在该产品上执行了另一个ajax调用,以检查是否已注册客户或来宾,并显示了用于注册的弹出窗口。 Now for registering customer, another ajax method is called which works properly but json data is not returned to the success and is directly shown on the page. 现在用于注册客户,调用了另一个ajax方法,该方法可以正常工作,但是json数据不会返回成功,而是直接显示在页面上。

Below id code for registering customer through popup 下面的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;
        });

Any solutions.I want json data to return back to success call. 任何解决方案。我都希望json数据返回到成功调用。

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

Try this, problem is your } is extra in your code, and missing dataType: 'json', if you use json . 试试这个,问题是您的}在代码中是多余的,并且如果使用json dataType: '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(); }
  }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM