调用MVC操作会导致ERR_CONNECTION_RESET 200(确定)

时间:2019-08-09 17:33:16

标签: jquery asp.net-core model-view-controller

我有一个网站可从服务中获取优惠券信息,当我执行ajax调用并尝试刷新页面时,该页面只会在我第一次调用时刷新,然后如果再次执行此过程,我会收到GET {{3 }} net :: ERR_CONNECTION_RESET 200(确定)或无法加载资源:net :: ERR_CONNECTION_RESET

我尝试将以下代码添加到Startup.cs中的ConfigureServices中:

services.AddMvc(
    options =>
      {
        var jsonOutputFormatter =
          options.OutputFormatters.SingleOrDefault(f => f.GetType() == typeof(JsonOutputFormatter)) as
            JsonOutputFormatter;
        if (jsonOutputFormatter != null)
        {

jsonOutputFormatter.PublicSerializerSettings.ReferenceLoopHandling =
            Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        }
      });

我尝试过使用window.location和重载功能,但都不起作用。

jQuery

  var url = new URL(window.location.href).pathname;
  var parameters = url.split("/");
  var token = parameters[parameters.length - 1];      

  function getCouponDiscount(couponId, token, couponCode) {
    $.ajax({
      url: "/purchase/confirm/coupon/" + couponId + "/" + token,
      data: "{}",
      beforeSend: function() {
        //Show image container
        $("#loader").css("display", "inline-block");
      },
      dataType: "json",
      success: function(data) {
        if (data.result === undefined) {
          console.log("No Valid Coupon");
          $("#response")
            .html(
              "The coupon code entered is not valid for this purchase.<br> Perhaps you used the wrong coupon code?");
        } else {
          $("#response").text("Valid Coupon. You will get " + parseFloat(data.result).toFixed(2) + "% off");
          applyDiscountToPurchase(token, parseFloat(data.result), couponCode);
        }
      },
      complete: function(data) {
        // Hide image container
        $("#loader").css("display", "none");
      },
      error: function(result) {
        $("#response").html("An error has occurred please try again");
      }
    });


function applyDiscountToPurchase(token, discount, couponCode) {
    $.ajax({
      cache: false,
      type: "GET",
      crossDomain: true,
      url: '@Url.Action("ApplyDiscountToPurchase", "Purchase")',
      data: {
        token: token,
        discount: discount,
        couponCode: couponCode
      },
      dataType: "json",
      success: function(data) {
        if (data) {
          window.location = data.redirectUrl;
        } else {
          console.log("Response False");
        }
      },
      complete: function(data) {

      }
    });
  }

控制器

[HttpGet("purchase/confirm/coupon/{couponId}/{token}")]
public async Task<IActionResult> GetCouponInformation(string couponId, string token)
{
  if (string.IsNullOrEmpty(couponId))
  {
    return RedirectToAction("Confirm");
  }
  // Get the discount amount
  var discountForCoupon = registerService.GetCouponDiscount(couponId);

  string result = await discountForCoupon;

  if (result.Contains("NO SUCH COUPON"))
  {
    return Json(new object[] { new object() });
  }
  else
  {
    return Json(discountForCoupon);
  }
}


public IActionResult ApplyDiscountToPurchase(string token, string discount, string couponCode)
{
  if (token == null || discount == null || couponCode == null)
  {
    RedirectToAction("Purchase");
  }

  var couponForCurrentPurchase = new CouponsCustomer
  {
    Coupon = couponCode,
    Discount = Convert.ToDecimal(discount),
    PurchaseComplete = false,
    Token = token,
  };

  // Save Data
  coupons.AddAsync(couponForCurrentPurchase);

  string redirectUrl = Url.Action("Confirm", new { token = token });

  return Json(new { redirectUrl });
}

预期的结果是在ajax调用后应用优惠券时重新加载页面。任何帮助将不胜感激。

0 个答案:

没有答案