2014-02-18 181 views
1

我試圖做GET使用jQuery AJAX功能的形式要求,但問題是,我沒有看到在URL中的參數:AJAX GET請求不顯示PARAMS在URL

這裏是代碼:

<form id="search_form" method="GET" action=""> 
    <input id="seach_input" type="text" name="q"> 
    <button id="search_btn" class="btn btn-warning" type="submit">Search</button> 
</form> 

這裏的Javascript代碼:

$("#search_form").on('submit', function(e) { 

    $.ajax({ 
     url: "index.html", 
     type: "GET", 
     data: {name: "hello"}, 
     success: function() { 
      console.log("success); 
     } 
    }); 

    e.preventDefault(); 
}); 

我提交後,我想要的網址看起來像這樣:

http://localhost/search.html?q=value 

我知道e.preventDefault();是參數沒有出現在URL中的原因,但我需要這樣做,而不需要刷新頁面,並且同時我希望在執行GET請求時看到參數。

請幫忙!

謝謝。

+0

你試過'返回FALSE'代替的preventDefault ?; –

回答

2

您可以使用window.history.pushState在Chrome,Safari,FF4 +和IE10等現代瀏覽器中完成所需操作。

一篇好文章是在這裏http://spoiledmilk.com/blog/html5-changing-the-browser-url-without-refreshing-page/

你需要添加像這樣到你的代碼。

window.history.pushState(「object or string」, 「Title」, 「/new-url」); 

此外,如果你不希望用戶能夠瀏覽當年使用replaceState代替

window.history.replaceState(「object or string」, 「Title」, 「/another-new-url」); 
+0

謝謝,我會這樣做 – kdureidy