通过GET关键字的空格被切断

时间:2012-09-05 21:35:36

标签: javascript jquery

我正在通过jquery调用从Django服务器加载一段HTML。

$('#search_result').load(url, function(){       
        ...
    });

网址创建如下:

url = url + '&' + keyword + '=' + value;    

只要关键字没有空格,它的工作正常,但像“固定出价”这样的东西会被切断为“固定”,这是一个问题。

/deals/?ajax&sales_term=Fixed

我应该用其他东西替换空间吗?如果我可以用Django识别为空格的字符替换它并在检索时将其转换回来,那将是很棒的。那真的很有效率。

3 个答案:

答案 0 :(得分:4)

您必须对您的网址进行编码。试试这个:encodeURIComponent。问题是,你需要用%20表示空间,即encodeURIComponent会处理的空间。

答案 1 :(得分:3)

只需使用jQuery.param

即可
url = {};
url[keyword] = value;

'?' + $.param(url); // ?keyword=value

答案 2 :(得分:0)

Url对您的网址进行编码,网址中的空格表示存在选择器,该选择器将用于过滤掉要加载到元素中的内容。

Loading Page Fragments
The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

We could modify the example above to use only part of the document that is fetched:

$('#result').load('ajax/test.html #container');
When this method executes, it retrieves the content of ajax/test.html, but then jQuery parses the returned document to find the element with an ID of container. This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded.

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as , , or  elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.

http://api.jquery.com/load/