Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

javascript - URL在jQuery中为AJAX请求编码字符串(URL Encode a string in jQuery for an AJAX request)

I'm implementing Google's Instant Search in my application.(我正在我的应用程序中实现Google的即时搜索。)

I'd like to fire off HTTP requests as the user types in the text input.(我想在用户输入文本输入时触发HTTP请求。) The only problem I'm having is that when the user gets to a space in between first and last names, the space is not encoded as a + , thus breaking the search.(我唯一遇到的问题是,当用户到达名字和姓氏之间的空格时,该空格未编码为+ ,从而破坏了搜索。) How can I either replace the space with a + , or just safely URL Encode the string?(如何用+代替空格,或者仅安全地对字符串进行URL编码?) $("#search").keypress(function(){ var query = "{% url accounts.views.instasearch %}?q=" + $('#tags').val(); var options = {}; $("#results").html(ajax_load).load(query); });   ask by Brian D translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try encodeURIComponent .(尝试encodeURIComponent 。)

Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).(通过用表示字符的UTF-8编码的一个,两个,三个或四个转义序列替换某些字符的每个实例,对统一资源标识符(URI)组件进行编码(对于由两个“代理”组成的字符,这将是四个转义序列字符)。) Example:(例:) var encoded = encodeURIComponent(str);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...