Ajax没有重定向到url

时间:2017-04-12 10:00:33

标签: javascript jquery asp.net ajax

我遇到问题要让ajax GET方法将我重定向到网址。 昨天我遇到了将数据从视图传递到控制器的问题。但现在它会起作用,但是ajax并没有将我重定向到我的视野。当我打开浏览器并单击调用此方法的按钮时,我看到没有出现错误。更好的是,如果我去“网络”,我可以看到所需的网址,如果我点击它,我会被重定向到正确的网址。 知道如何解决这个问题吗?我是否必须对Succes funcion做些什么?

打印屏幕: enter image description here When manually click on the url

 function ShowTasks() {
    //Dear Stackoverflow > This works, this is for selecting a row in the table
    var $selectedRow = $(".highlight");
    if ($selectedRow.length == 1) {
        var dcColumn = 0;
        var rdColumn = 1;
        var shopdoccodeColumn = 4;

        //assigning name to the colomn value
        var id = $selectedRow[0].children[dcColumn].innerText.trim();
        var regdate = $selectedRow[0].children[rdColumn].innerText.trim();
        var shopdoccode = $selectedRow[0].children[shopdoccodeColumn].innerText.trim();

        //ajax
        if (id && regdate && shopdoccode) {
            $.ajax({
                type: 'GET',
                url: '@Url.Action("delivery", "service"@*, new { id = "id", shopdoccode = "shopdoccode", regdate = "regdate" }*@)',
                data: { id, regdate, shopdoccode },
                success: function (data) {
                    if (data.success) {
                        console.log("Succes");
                    }

                },
                error: function (data) {
                    console.log("Error");
                }
            });
        }
    }
}

的ServiceController:

 [Route("/service/delivery/{id}/{shopdoccode}/{regdate}")]
        public ActionResult Delivery(string id, string shopdoccode, string regdate)
        {
            string numberPlate = Session[NumberPlateSessionVar] as string;
            if (string.IsNullOrWhiteSpace(numberPlate))
            {
                return RedirectToAction("LicensePlate");
            }
            IList<ServiceOrder> getWorkReportFormData = ServiceRepository.GetWorkReportForm(id, shopdoccode, regdate);
            IList<ServiceDelivery> deliveredTasks = ServiceRepository.DeliveredTasks(id);
            ContactsModel contactsModel = new ContactsModel();
            IList<DeliveredTaskModel> delTasksModels = new List<DeliveredTaskModel>();
            string signature = string.Empty;
            if (deliveredTasks.Count > 0)
            {
                contactsModel.Name = deliveredTasks[0].ContactName;
                contactsModel.Email = deliveredTasks[0].Email;
                contactsModel.Phone = deliveredTasks[0].PhoneNumber;
                delTasksModels = deliveredTasks.Select(x => new DeliveredTaskModel()
                {
                    Description = x.Description,
                    Info = x.PartCode,
                    Qty = x.CalcQty
                }).ToList<DeliveredTaskModel>();
                signature = Convert.ToBase64String(deliveredTasks[0].Signature);
            }
            DeliveryModel model = new DeliveryModel()
            {
                DossierCode = id,
                Contacts = contactsModel,
                DeliveredTasks = delTasksModels,
                Signature = signature
            };
            if(getWorkReportFormData.Count > 0)
            {
                Console.WriteLine("Test");
            }
            return View(model);
        }

之前的问题,可能是一个小亲戚。 Url action parameters using Ajax

2 个答案:

答案 0 :(得分:0)

您的网址似乎不正确。尝试以下代码,并将URL和data:参数更改为以下代码。 (如 //******** 所述)

if (id && regdate && shopdoccode) {
            $.ajax({
                type: 'GET',
                url: '@Url.Action("delivery", "service")',    //  ********               
                data: { "id": id, "shopdoccode ": shopdoccode , "regdate": regdate }, // ********
                success: function (data) {
                    if (data.success) {
                        console.log("Succes");
                    }

                },
                error: function (data) {
                    console.log("Error");
                }
            });
        }

答案 1 :(得分:0)

使用此解决了我的问题:

 window.location.href = ("service/delivery?id=" + id + "&toolcode=" + toolcode + "&regdate=" + regdate); 

之后的Ajax方法