2014-02-18 121 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