2

我想从视图中点击控制器中的一个动作,让它返回一个结果。但是我总是收到“请求失败”,然后控制器中的操作代码执行。我试图在视图中使用它:

$("#State").change(function() {
        if ($(this).val() != "Va") {
            $.ajax({
                url: '@Url.Action("ProcessOrder","Checkout")',
                type: 'POST',
                success: function(result) {
                    if(result.success) {
                        alert("good " + result);    
                    } else {
                        alert("bad " + result);
                    }
                },
                error: function() {
                    alert("request failed");
                }
            });

        } else {
            formSwitcher($(this).val());
        }

    });

这在控制器中:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult ProcessOrder()
    {
         string sURL = @"https://someUrl/?OrderId=" + Id ;
         var badUrl = string.Empty;
            try
           {
               Response.Redirect(sURL, true);
               HttpWebRequest request = null;
               HttpWebResponse httpWebResponse = null;
               request = HttpWebRequest.Create(sURL) as HttpWebRequest;
               request.Method = "GET"; // Supports POST too
               httpWebResponse = (HttpWebResponse)request.GetResponse();
               return Json(new { success = true });
           }
           catch (Exception ex)
           {
                badUrl = "~/Shared/error.aspx";
           }
           return Json(new { success = false, error = badUrl });
    }

我究竟做错了什么?

4

1 回答 1

1

所以问题是 Response.Redirect(sURL, true); 正在抛出一个异常,该异常在被 try catch 捕获之前被 ajax 捕获。结果ajax说有故障,代码继续运行。

于 2013-04-29T20:51:17.430 回答