8

Hi i have a simple question. I want to know how to pass a query string into a query parameter using jquery.

function loadPage(queryString) {
    jQuery("#divId").load("myurl/action?param=" + queryString);
}

queryString could be like "1,2,3,4" or "testing 1 2 3".

When I tried it only the first parameter is their. I want to be able to pass in a sentence, or a paragraph.

Chun ping Wang
  • 3,879
  • 12
  • 42
  • 53
  • try using JavaScripts built in `encodeURI(str)` – ShaneA Feb 07 '13 at 17:57
  • 2
    I think you want to use encodeURIComponent: http://stackoverflow.com/questions/6544564/url-encode-a-string-in-jquery-for-an-ajax-request – AaronLS Feb 07 '13 at 17:57
  • 1
    Note that encodeURI will keep slashes, and thus if you are trying to pass it in a param, the slashes will break the param. This is why you should use encodeURIComponent in this case, in other cases where you have a path and want to preserve slashes, then use encodeURI. – AaronLS Feb 07 '13 at 18:00
  • yeah thanks, i needed to keep the "/" – Chun ping Wang Feb 07 '13 at 22:53

2 Answers2

19

try

function loadPage(queryString) {
    jQuery("#divId").load("myurl/action?param=" + encodeURIComponent(queryString));
}
marty
  • 4,005
  • 22
  • 19
5

Use encodeURIComponent():

function loadPage(queryString) {
    jQuery("#divId").load("myurl/action?param=" + encodeURIComponent(queryString));
}