Ajax - mvc请求总是失败

时间:2013-04-25 22:24:23

标签: ajax asp.net-mvc json

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

$("#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 });
    }

我做错了什么?

1 个答案:

答案 0 :(得分:1)

所以问题是Response.Redirect(sURL,true);抛出了一个异常,它被ajax捕获,之后它被try catch捕获。结果是ajax说失败了,代码继续运行。