使用ajax post方法无法重定向查看页面?为什么? [英] View page could not be redirect by using ajax post method? Why?

查看:78
本文介绍了使用ajax post方法无法重定向查看页面?为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Controller:
[HttpPost]
        public ActionResult View1(string SelectedVal)
        {
            IList<string> split1;
            split1 = SelectedVal.Split(new[] { '\n' });

            ViewBag.Code = split1[0];
            ViewBag.Desc = split1[2];
            
            return View(); 
        }
JS:
<script type="text/javascript">
    function onClick(e) {

        //alert(e.item[0].outerText)
        var SelectedVal = e.item[0].outerText;
        //alert(SelectedVal)
        $.ajax({
            url: "/TTSCancel/View1",
            data: JSON.stringify({ SelectedVal: SelectedVal }),
            dataType: "json",
            type: "POST",
            contentType: 'application/json; charset=utf-8'
        });
    }
</script>
View:
@model List<TTS.Models.CancelModel>
<h3>Code :</h3>
<label class="labelclass">@ViewBag.Code</label>
<br />
<h3>Decsription :</h3>
<label class="labelclass">@ViewBag.Desc</label>
<br />

推荐答案

.ajax({
url: / TTSCancel / View1
data : JSON .stringify({SelectedVal:SelectedVal}),
dataType: json
类型: POST
contentType:' 应用/ JSON; charset = utf-8'
});
}
< / script>
查看:
@model列表< TTS.Models.CancelModel>
< h3>代码:< / h3 >
< label class = labelclass> @ ViewBag.Code < / label >
< br />
< h3> Decsription:< / h3 >
< label class = labelclass> @ ViewBag.Desc < / label >
< br />
.ajax({ url: "/TTSCancel/View1", data: JSON.stringify({ SelectedVal: SelectedVal }), dataType: "json", type: "POST", contentType: 'application/json; charset=utf-8' }); } </script> View: @model List<TTS.Models.CancelModel> <h3>Code :</h3> <label class="labelclass">@ViewBag.Code</label> <br /> <h3>Decsription :</h3> <label class="labelclass">@ViewBag.Desc</label> <br />


您的所有代码都在做向网址发出请求。该请求将返回View的html,但是你没有对结果做任何事情,只是忽略它。如果您想将用户实际重定向到该视图,则不应使用ajax,只需使用普通表单即可。如果您想要一个链接类型的流程而不是表单提交按钮,您可以使用您的js通过点击链接提交表单。如果你不想引导用户,只是想在你的页面上显示html,那么你需要使用ajax调用的success事件将html注入你的页面。
All your code is doing is making a request to the url for the action. The request will return the html for the View, but you're not doing anything with the result, just ignoring it. If you want to actually redirect the user to that view you shouldn't use ajax, just use a normal form. If you want a link-type process rather than a form submit button you can use your js to submit the form on the click of your link. If you don't want to direct the user away but to simply show the html on your page somewhere then you need to use the "success" event of your ajax call to inject the html somewhere into your page.


这篇关于使用ajax post方法无法重定向查看页面?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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