2016-05-08 57 views
4

我遇到的問題是&X-Requested-With=XMLHttpRequest&_=1462736803425不斷追加到我的url,因爲它是查詢字符串的一部分。有沒有一種方法可以阻止它成爲一個查詢字符串,並在不做'黑客'的情況下做Ajax.BeginForm在ajax請求期間從查詢字符串url中刪除'X-Requested-With'

@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

回答

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); 
} 

編輯。新的答案基於邁克評論:

@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="[email protected]"> 
       @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和不能使用助手。

3

在不更改源文件"jquery-ajax-unobtrusive.js"的情況下,這並不容易。這裏的問題是在線117:

options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" }); 

這是在發送請求之前添加數據。這有點令人討厭,特別是在我看來,這個值應該是一個HTTP標頭,而不是數據的一部分。

還有可能是一種通過向表單的ajax請求添加beforeSend選項來刪除它的方法。也就是說,你要定義一個函數:

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 HTML屬性與價值removeExtraData到表單中。我還沒有證實,而且自從使用WebForms已經過去幾年了,所以我沒有安裝程序來測試。

_=1462736803425 URL的一部分被jQuery自動附加,因爲該請求被設置爲不被緩存。顯然,您可以通過向名稱添加HTML屬性來刪除名稱,其名稱爲data-ajax-cache,值爲true。當然,這可能最終會緩存您的請求,這可能不是需要的。

+0

感謝您的回答,清除了一些東西。 'settings'雖然沒有'data'的屬性,但我找不到要刪除的標題。 –

+0

我能想到的唯一方法就是不使用'Ajax.BeginForm'。使用'Html.BeginForm'並直接使用jQuery來提交帶有Ajax的表單。儘管如此,這是相當多的工作。 –

+0

感謝您的幫助。真的很煩人,這甚至是使用ajax助手的問題。 如果沒有更好的答案出現,我會給你獎金。 –

0

我想你可以通過覆蓋url路徑來解決這個問題。

在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>); 
    } 

這樣可以解決你的問題,但我真的不能說,如果這是一個不錯的辦法。

+0

我沒有'BeginRequest()'來覆蓋Global.asax .. –

+0

這是MVC管道中的一個方法。如果您沒有自定義implmentation,則可以直接使用它,因爲它也調用基本函數。 –

相關問題