在javascript中使用ajax重定向到另一个视图 [英] Redirect to another view using ajax in javascript

查看:82
本文介绍了在javascript中使用ajax重定向到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Javascript的新手。在以下场景中我需要帮助:



我的视图中有2个下拉列表,还有一个Anchor(a)元素。单击此锚元素,我想重定向到另一个视图(基本上必须调用另一个动作函数),但我需要传递在这2个下拉列表中选择的当前值。简而言之,我需要调用另一个动作函数并传递下拉中选择的2个当前值。



我尝试过:



我编写了以下Ajax代码,但总是会出现错误部分:

I am new to Javascript. I need help in the following scenario:

I have 2 dropdowns in my view, and a Anchor(a) element. On click of this anchor element, I want to redirect to another view(basically have to call another action function), but I need to pass the current values which are being selected in those 2 drop downs. In short, I need to call another action function and pass the 2 current values selected in drop downs with it.

What I have tried:

I have written the following Ajax code, however it is always going to the error part:

$.ajax({
    type: 'GET',
    url: Url.Action("ActionMethod", "MyController"),
    data: {
        param1: DD1Value,
        param2: DD2Value,
        param3: xyz
    },
    success: function (data) {
        window.location.href = data.redirecturl;
    },
    error: function () {
        alert('error happened');
    }
}); 





控制器:



Controller:

[HttpGet]
public ActionResult ActionMethod(DateTime param1, DateTime param2, string param3)
{
//Do some work
//Return View
}





亲切帮助。



Kindly Help.

推荐答案

.ajax({
type:' GET'
url:Url.Action( ActionMethod,< span class =code-string> MyController),
data:{
param1:DD1Value,
param2:DD2Value,
param3:xyz
},
成功: function (数据){
window location .href = data.redirecturl;
},
错误: function (){
alert('' 错误发生');
}
});
.ajax({ type: 'GET', url: Url.Action("ActionMethod", "MyController"), data: { param1: DD1Value, param2: DD2Value, param3: xyz }, success: function (data) { window.location.href = data.redirecturl; }, error: function () { alert('error happened'); } });





控制器:



Controller:

[HttpGet]
public ActionResult ActionMethod(DateTime param1, DateTime param2, string param3)
{
//Do some work
//Return View
}





亲切帮助。



Kindly Help.


试试这个..

注意内联评论



Try this..
Note the inline comments

function fungo() {
        var date = new Date(); // Javascript date object 
        var link = '@Url.Action("ActionMethod", "Home")';  // url should be enclosed by single quotes.
        var args = {
            param1: date.toISOString(),  // make sure that the date is in Javascript date object and converted to ISO string for proper casting in c#
            param2: date.toISOString(),
            param3: 'somevalue'
        };


.ajax({
type: GET
url:link, // 您的操作的网址
数据:args, // 参数(如果可用)
dataType: json,
成功: function (数据){

< span class =code-sdkkeyword> window 。 location .href = data.redirecturl; // 您的操作应返回具有[redirecturl]属性的对象

} ,
错误: function (httpRequest,textStatus,errorThrown){ // 详细错误消息
alert( 错误: + textStatus + + errorThrown + + httpRequest);
}
});

}
.ajax({ type: "GET", url: link, // url of your action data: args, // parameters if available dataType: "json", success: function (data) { window.location.href = data.redirecturl; // your action should return an object having [redirecturl] property }, error: function (httpRequest, textStatus, errorThrown) { // detailed error messsage alert("Error: " + textStatus + " " + errorThrown + " " + httpRequest); } }); }





控制器:





Controller:

[HttpGet]
    public ActionResult ActionMethod(DateTime param1, DateTime param2, string param3)
    {
        return Json(new { redirecturl = "http://www.codeproject.com/" }, JsonRequestBehavior.AllowGet);

    }


这篇关于在javascript中使用ajax重定向到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆