为什么我们不能在ajax请求中访问TempData? [英] Why we cannot accessTempData in ajax request?

查看:119
本文介绍了为什么我们不能在ajax请求中访问TempData?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们无法在ajax请求中访问/获取TempData。



例如:



控制器:



Why we cannot access/get TempData in ajax request.

For example:

Controller:

TempData["MyMessage"] = MyMessage;
 return Json(true, JsonRequestBehavior.AllowGet);

< br $>


查看:



$ .ajax(

{

url:'@ Url.Action(someAction)',

dataType:'json',

data:$(form)。serialize (),

类型:'POST',

成功:函数(结果){

value = TempData [MyMessage ]; //为什么这是不可能的



},

错误:function(xhr){

alert(xhr.statusText);

}

});





我知道我可以使用json获取我的消息,但我想知道为什么tempdata不是accessib的原因le in ajax request。



View:

$.ajax(
{
url: '@Url.Action("someAction")',
dataType: 'json',
data: $("form").serialize(),
type: 'POST',
success: function (result) {
value=TempData["MyMessage"];//why this is not possible

},
error: function (xhr) {
alert(xhr.statusText);
}
});


I know i can get my message using json , but i want to know the reason that why the tempdata is not accessible in ajax request.

推荐答案

.ajax(

{

url:'@ Url.Action( someAction)',

dataType:'json',

data:
.ajax(
{
url: '@Url.Action("someAction")',
dataType: 'json',
data:


(form)。serialize() ,

类型:'POST',

成功:函数(结果){

value = TempData [MyMessage]; //为什么这是不可能的



},

错误:函数(xhr){

alert(xhr.statusText);

}

});





我知道我可以使用json获取我的消息,但我想知道为什么在ajax请求中无法访问tempdata的原因。
("form").serialize(),
type: 'POST',
success: function (result) {
value=TempData["MyMessage"];//why this is not possible

},
error: function (xhr) {
alert(xhr.statusText);
}
});


I know i can get my message using json , but i want to know the reason that why the tempdata is not accessible in ajax request.


由于TempData on,这是不可能的存在于服务器端编码中,并且您尝试在javascript中在客户端上使用它。如果js直接在视图上你可以做类似的事情



It's not possible because TempData only exists in server-side coding and you're trying to use it on the client in javascript. If the js is directly on the view you could do something like

value='@TempData["MyMessage"]';





只有在将页面发送到客户端之前设置TempData时才会起作用,如果TempData设置在客户端,它将无法工作ajax方法。这是因为你的视图正在生成发送到客户端的静态html,所以如果你查看源代码,你会看到你的js看起来像





That will only work if the TempData was set before the page was sent to the client though, it won't work if TempData is set in the ajax method. This is because your view is generating static html that is sent to the client, so if you look at the source you'll see your js look like

success: function (result) {
 value='Whatever is in TempData when the page loads';





如果TempData中没有任何内容则它看起来像





If nothing is in TempData then it will look like

success: function (result) {
 value='';





所以无论你的ajax方法中发生了什么,你的js中的成功事件总是将值设置为同一个东西。记住js运行发送到客户端的任何内容,而不是来自你的cshtml视图,你的.net代码没有在浏览器内部运行,浏览器的html不是你的服务器代码可以自由交互的实时对象。 />


但是你应该做的是让你的请求返回一个有两个值的对象,成功和消息





So no matter what happens inside your ajax method, the success event in your js is always going to set value to be the same thing. Remember js runs off whatever is sent to the client, not from your cshtml view, and your .net code isn't running "inside" the browser, the browser's html is not a live object your server code can freely interact with.

However what you should do is make your request return an object with two values, the success and the message

return Json(new{success=true, value=MyMessage}, JsonRequestBehavior.AllowGet);





in你的js;





in your js;

success: function (result) {
    success = result.success;
    value=result.value;


这篇关于为什么我们不能在ajax请求中访问TempData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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