有没有办法判断请求是否是Angular(1.1.5)$资源请求.我正在为这种类型的请求寻找"Request.IsAjaxRequest()"方法.
我正在查看这个在HandleUnauthorizedRequest中重写AuthorizeAttribute我需要将上下文结果设置为某些json(如果是Ajax或角度请求或其他内容).
让我先解释一下我尝试过的所有可能的解决方案.我现在已经jquery-unobtrusive-ajax.min.js在我的脚本文件夹中并将其添加到一个包中.我已尝试在视图页面本身上引用它,同时还有_Layout.cshtml页面,这就是我当前拥有该包的方式:
//BundleConfig.cs
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-1.10.2.min.js",
"~/Scripts/modernizr-2.6.2.js",
"~/Scripts/jquery.validate.min.js",
"~/Scripts/jquery.unobtrusive-ajax.min.js"));
Run Code Online (Sandbox Code Playgroud)
在主布局视图页面中引用此包,我的所有其他视图都派生自:
//_Layout.cshtml (at bottom of view, right before hitting </body></html>)
@Scripts.Render("~/bundles/jquery")
Run Code Online (Sandbox Code Playgroud)
最后在web.config中,我已将这些键添加到应用程序设置中:
//Web.config
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
现在已经说明了,这就是我的Ajax调用的样子.我试图从一个独立的页面返回一些数据并将其替换为主页面中的内容,我认为相当简单:
@Ajax.ActionLink("Edit", "Index", "WeeklyTarget",
new {id = item.WeeklyTargetId},
new AjaxOptions {HttpMethod = "GET", UpdateTargetId = "hoursEdit", InsertionMode = InsertionMode.Replace})
Run Code Online (Sandbox Code Playgroud)
从索引视图中单击此操作链接时,将从控制器调用此链接:
public ActionResult Index(int? id, IEnumerable<WeeklyTarget> selection )
{
if (Request.IsAjaxRequest())
{
// return the partial view here
}
}
Run Code Online (Sandbox Code Playgroud)
但正如我的标题所述,Request.IsAjaxRequest()仍将总是返回虚假.是什么赋予了???请帮忙......我已经筋疲力尽了所有的想法,调整和解决方法.我甚至尝试清理我的缓存,清洁解决方案等.
我的get路线中有一条如下路线.如何检查请求是xhr还是正常的http请求?
我想要做的是重定向,如果它http,如果xhr它将转到下面controller和action
#if xhr
get '/search' => 'pages#search'
#if http
get '/search' => redirect("/")
Run Code Online (Sandbox Code Playgroud)
我怎么能这样做routes.rb???
根据BoraMa的回答,这里是routes.rb
Rails.application.routes.draw do
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
devise_for :users,
:path => '',
:path_names => {:edit => 'profile'},
:controllers => {:registrations => 'registrations'}
resource :pages, only: [:edit] do
collection do
patch 'update_password'
end
end
resources :conversations, only: [:create, :index] do
resources :messages, only: [:create, :index]
end
resources :notifications do
collection do
post :mark_as_read …Run Code Online (Sandbox Code Playgroud)