tags:

views:

174

answers:

3

I'm making an ajax request and I have some problems, this is my jquery code:

var url = "http://www.domain.com/SearchService.svc/search?keyword=my search keywords";
    $.ajax({
        type: "GET",
        url: url,
        dataType: "json".......
.....

When making this request I sometimes have blank spaces in my search (var url) and then the keywords get cutted so in the example above for example it just searches for "my". I understand this is a quite simple question and that must be an easy solution. Just couldn't find a solution...

Thanks for your help!

+1  A: 

Just convert whitespaces in url format. For each whitespace char use %20 instead:

var url = "http://www.domain.com/SearchService.svc/search=my%20search%20keywords";
    $.ajax({
        type: "GET",
        url: url,
        dataType: "json".......
.....

Or if you want to make it in a more automatic way just use the javascript function escape() like Felix suggested.

rogeriopvl
there's a javascript function that does this, I think it's called escape.
roe
@row yes you're correct. I couldn't recall the name of the function.
rogeriopvl
+1  A: 

You can escape the search keyword in the URL but the right thing to do is to add search as a parameter to the ajax request.

var url = "http://www.domain.com/SearchService.svc/search=my search keywords";
    $.ajax({
        type: "GET",
        url: url,
        data: { "search" : "my search keywords" }
        dataType: "json".......
Pekka
it appears he's not using a standard query-string, so that wouldn't work
roe
Ahh I see now, you're right.
Pekka
+2  A: 

Use

var url = escape("http://www.domain.com/SearchService.svc/search=my search keywords");

This encodes the URL and converts the blanks and any other 'unsafe' character for URL usage.

Felix Kling