【问题标题】:asp.net mvc3 302 found error on $.get callasp.net mvc3 302 在 $.get 调用上发现错误
【发布时间】:2013-09-20 06:52:13
【问题描述】:

asp.net mvc3 中的 302 found 错误代码是什么?我正在尝试做一个简单的 jquery $.get 调用以从服务器获取当前时间戳。

以下是代码:

javascript

var url = '/Utility/GetTimeStamp';
$.get(url, function(data){  $("#curr_time").val(data); });

c#mvc

public ActionResult GetTimeStamp()
{
   string time_stamp = DateTime.Now.ToString("o");
   return Content(time_stamp);
}

【问题讨论】:

  • 302 表示数据在缓存中
  • 我该如何摆脱这个错误?
  • 做到了,还是一样..
  • 是的。我也这样做了 var response = filterContext.HttpContext.Response; response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); response.Cache.SetValidUntilExpires(false); response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); response.Cache.SetCacheability(HttpCacheability.NoCache); response.Cache.SetNoStore();
  • 302 不是重定向吗?

标签: c# jquery asp.net asp.net-mvc asp.net-mvc-3


【解决方案1】:

如果控制器的名称是 UTILITY.. 并且当前您在那里

试试这个代码

javascript

   var url = 'GetTimeStamp';
    $.post(url
     ,{}
     , function(data)
        { 
           $("#curr_time").val(data); 
        });

C#MVC

[HttpPost]
public JsonResult GetTimeStamp()
{
   string time_stamp = DateTime.Now.ToString("o");
   return Json(time_stamp);
}

它返回一个 json 结果。

【讨论】:

    【解决方案2】:

    你是说 304?
    1、给你的ajax请求添加一个随机数或数据时间($.get)参数
    2、使用$.ajax({cache:false})
    3、在服务器端代码中设置无缓存,如
    HttpContext.Response.Cache.SetNoStore();

    【讨论】:

      【解决方案3】:

      你的 web.config 中有类似的东西吗?

      <location path="assets">
          <system.web>
            <authorization>
              <allow users="*" />
            </authorization>
          </system.web>
        </location>
        <location path="scripts">
          <system.web>
            <authorization>
              <allow users="*" />
            </authorization>
          </system.web>
        </location>
      

      我遇到了这个问题,所有未经身份验证的请求都被重定向到登录页面。尝试将 ajax url 粘贴到新的浏览器窗口中,看看会得到什么。

      【讨论】:

        猜你喜欢
        • 2013-06-01
        • 2014-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-18
        相关资源
        最近更新 更多