我想从视图中点击控制器中的一个动作,让它返回一个结果。但是我总是收到“请求失败”,然后控制器中的操作代码执行。我试图在视图中使用它:
$("#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 });
}
我究竟做错了什么?