删除' X-Requested-With'来自ajax请求期间的查询字符串url

时间:2016-05-08 19:53:35

标签: javascript c# jquery ajax asp.net-mvc

我遇到的问题是&X-Requested-With=XMLHttpRequest&_=1462736803425会一直附加到我的网址,因为它是查询字符串的一部分。有没有办法让我可以阻止它成为一个查询字符串并且在没有做“黑客”的情况下执行Ajax.BeginForm

@using (Ajax.BeginForm("Search", "Filter", new { Area = "Music" }, new AjaxOptions { HttpMethod = "Get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "body-wrapper", OnSuccess = "updateHistory" }, new { @id = "search-form" }))
{
       <div>
           <i class="fa fa-search"></i>
           <input type="search" placeholder="Search" id="search" name="searchString" />
       </div>
}

public ActionResult Search(string searchString)
{
    //do stuff
    return PartialView();
}

在局部视图页面上,我获得路径和查询:

@Html.HiddenFor(x => Request.Url.PathAndQuery)

Request.Url.PathAndQuery的值为:http://localhost:1526/Music/Search?searchString=maid&X-Requested-With=XMLHttpRequest&_=1462736803425

然后使用History.js更新网址:

function pushState(target) {
    manualStateChange = false;
    History.pushState(null, null, $("#Request_Url_PathAndQuery").val());
}

enter image description here

3 个答案:

答案 0 :(得分:3)

在不改变"jquery-ajax-unobtrusive.js"的源文件的情况下,这并不容易。这里的问题在第117行:

beforeSend

在发送请求之前添加该数据。在我看来,那种令人讨厌的东西,尤其是那个值应该是HTTP标题,而不是数据的一部分。

可能是一种通过在表单的ajax请求中添加function removeExtraData(jqXHR, settings) { if (settings.data.hasOwnProperty('X-Requested-With')) { delete settings.data['X-Requested-With']; // or settings.data['X-Requested-With'] = undefined; } } 选项来删除它的方法。也就是说,您想要定义一个函数:

data-ajax-begin

然后,您可以通过向表单添加值removeExtraData的{​​{1}} HTML属性来使用该属性。我还没有证实这一点,而且自从我使用WebForms以来已经过了几年,所以我没有设置测试。

URL的_=1462736803425部分由jQuery自动附加,因为请求被设置为不被缓存。显然,您可以通过在名称为data-ajax-cache且值为true的表单中添加HTML属性来摆脱这种情况。当然,这可能最终会缓存您的请求,这可能是不可取的。

答案 1 :(得分:0)

我认为您可以通过覆盖网址路径解决此问题。

在Application_BeginRequest()中,您可以检查所需的任何条件,并使用context.RewritePath()并删除不必要的参数。

这样的事情:

    protected override void Application_BeginRequest(object sender, EventArgs e)
    {
        base.Application_BeginRequest(sender, e);
        var urlpath = HttpContext.Current.Request.Url.PathAndQuery; //Modify this based on condition
        if(<condition>)
            Context.RewritePath(<updatedpath>);
    }

这可能会解决您的问题,但我不能说这是否是一个好方法。

答案 2 :(得分:0)

我带走的是这个,虽然这是一个黑客而且不是很理想,所以如果有人找到更好的方法请说。它从url中删除查询字符串参数,它起作用,因为xhr总是在末尾附加。

function pushState(target) {
    //Remove the queryString parameter of xhr that gets appened in 117 of ajax-unobtrusive.js
    var index = target.indexOf("?X-Requested-With");
    if (index === -1) 
        index = target.indexOf("&X-Requested-With");

    target = target.replace(target.substring(index), "");

    manualStateChange = false;
    History.pushState(null, $("input#Title").val(), target);
}

修改。基于Mike评论的新答案:

@using (Html.BeginForm("Index", "Search", new { area = "Media" }, FormMethod.Get, new { @id = "search", data_url = Url.Action("SetSearchTags", "Search", new { Area = "Media" }) }))
{
    <i class="fa fa-search"></i>
    <input type="search" placeholder="Search" id="search" name="searchString" />

    <div id="filter" name="filter">
        <span><strong>Syntax</strong></span>

        @foreach (var item in SearchContext.SearchFilters)
        {
            <div id="filter-@item">
                @Html.DisplayFor(x => item)
            </div>
        }
    </div>
}

$("form#search").submit(function (event) {
    event.preventDefault();

    var $form = $(this);
    var searchString = $form.find("input[type='search']").val();
    var url = $form.attr("action");
    var method = $form.attr("method");

    $.ajax({
        url: url,
        data: { searchString: searchString },
        method: method,
        success: function (data) {
            $('#body-content').html(data);
            updateHistory(true);
        }
    });
});

function updateHistory(useQuery) {
    var path = (useQuery === true) ? $("#Request_Url_PathAndQuery").val() : $("#Request_Url_AbsolutePath").val();

    pushState(path);
};

基本上,只要我需要查询字符串参数,我只需要在jquery中手动执行它而不使用帮助程序。