在jQuery的浏览器URL中获取QueryString获取请求 [英] Get QueryStrings in browser url in jquery get request

查看:422
本文介绍了在jQuery的浏览器URL中获取QueryString获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jquery发送一个get请求,但是get函数没有将查询字符串放在地址栏中.我尝试将async设置为false,但仍然无法正常工作.

I would like to send a get request with jquery but the get function does not put the query strings in the address bar.I tried setting async to false but it still does not work.

$("#searchForm").submit(function (event) {
            var x = $("#category").serialize();
            $.get("Home/test", x, function (data) {               
                alert(data);
            });
            event.preventDefault();
            });

上面的代码不会像普通的get请求那样将查询字符串放在地址栏中.

The code above does not put the query strings in the address bar like a normal get request would.

因此,我应该在浏览器地址栏中输入Home/Text/?category = laptop

hence i should have in the browser address bar something like Home/Text/?category=laptop

例如

欢迎使用其他任何解决方案来指定我可以在地址栏中输入的查询字符串.

any other solutions to specify what querystrings i can put in the address bar are welcome.

form id="searchForm" action="@Url.Action("Index")" method="get">    

    <input type="text" name="search" id="search />
    <div id="price">
        <input type="text" id="PriceMin" name="PriceMin" />
        <input type="text" id="PriceMax" name="PriceMax" />
    </div>
    <input type="submit" value="submit"/>
</form>

作为示例,我不想输入

?search = volvo& PriceMin =& PriceMax =

?search=volvo&PriceMin=&PriceMax=

换句话说,不应将null的输入字段放在获取请求的url中.

In other words, the input fields that are null should not be placed in url on a get request.

thx.

发现了一些东西,并且可以正常工作,为什么我以前没有想到过

Found something and its working why i didnt think of this before is beyond me

$("#searchForm").submit(function (event) {
            if ($("#PriceMin").val() == "") { $("#PriceMin").prop("disabled", true); alert("isnull");}
            });  

如果在发送表单之前输入字段为",则禁用输入字段.由于元素被禁用,因此不发送查询字符串"priceMin".

i disable the input field if it is "" just before the form is sent.Thus the query string of "priceMin" is not sent because the element is disabled.

推荐答案

地址栏不变的原因是,当您使用javascript发出AJAX请求时,该请求是在后台进行的. AJAX的全部目的是在不离开当前页面的情况下发出请求.如果您更改地址栏中的网址,将触发整个页面重新加载.因此,您要实现的目标是不可能的.您可以使用网址的片段部分(#之后的部分).您可以在不导致浏览器离开的情况下修改网址的这一部分.请看以下文章: http://ajax.rswebanalytics .com/seo-for-ajax/#!ajax-crawlable-urls

The reason why the address bar doesn't change is because when you make an AJAX request with javascript, this request is made in the background. The whole point of AJAX is to make requests without navigating away form the current page. If you change the url in the address bar that would trigger the entire page to be reloaded. So what you are trying to achieve is impossible. You could use the fragment portion of the url (the part that follows the #). You could modify this part of the url without causing the browser to navigate away. Take a look at the following article: http://ajax.rswebanalytics.com/seo-for-ajax/#!ajax-crawlable-urls

因此,您可以使用以下网址:

Thus you could have the following url:

http://example.com/Home/Text/#category=laptop

为了在javascript中处理网址的片段部分,您可以使用

In order to manipulate the fragment portion of the url in javascript you could use the window.location.hash property. Bear in mind though that this part of the url is never sent to the server. It is only intended to be used on the client.

就第二个有关空字符串的问题而言,没有办法用纯HTML来实现.您将必须编写javascript.基本上,在这里您将必须订阅表单的.submit事件,并手动构建您将重定向到的目标URL,并排除具有空值的参数.然后使用window.location.href属性手动重定向到该URL.另外,不要忘记通过返回false来取消提交的默认操作.

As far as your second question about the empty strings is concerned, there's no way to achieve that in pure HTML. You will have to write javascript. Basically here you will have to subscribe to the .submit event of the form and manually build the target url that you will redirect to and exclude parameters that have empty values. Then manually redirect to this url using the window.location.href property. Also don't forget to cancel the default action of the submit by returning false.

这篇关于在jQuery的浏览器URL中获取QueryString获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆