tags:

views:

323

answers:

5

I'm doing a search within list of people. And I want it to show results on the fly and so it does. But there is one link that I need and it should look like this:

chatid=18&userid=45&create=new

but after the results are displayed through this:

$.get('/ajax.php', {sec: 'search_users', ajax: 1, search_for: $(this).val()}, function(data)
{
 $(".rBoxContentStaff").html(data);
});

I get this result:

chatid=18&userid=45&create=new

And the link doesn't work. This seems to happen in html() and also append().

I found no solution for this so I had to change the triggering of the link.

A: 

did you alert data to check if .html() replaces the symbols or maybe the ajax request?

janoliver
if I alert data it look great but after html() it all goes wrong
Belgurinn
A: 

Hi,

html special chars when requested by ajax, don't get interpreted by the browser .. that's why the links came out that way. One thing you can do is a sstring replace in javascript:

data = str.replace(/&/, "&");
yoda
data returns everything right if I alert it, this happens after html() it seems.
Belgurinn
I already tried this
Belgurinn
Then don't return the full link, return it as a json array, for example
yoda
+1  A: 

You said you are trying to set the link path correct?
If so try this

$(".rBoxContentStaff").attr("href", data);

the .html() is changing the & to an html & (&) but using .attr("href") sets the link path and it works with &s as well

jmein
that I know I'm looking for the solution for this.
Belgurinn
+1  A: 

Instead of doing this:

$(".rBoxContentStaff").html(data); });

Try this:

$(".rBoxContentStaff").append(data); });

text() escapes html characters ... I can't find anything about html() escaping characters (and indeed, its documentation seems to indicate otherwise.
However, after testing with alert at jmein's suggestion, it does encode special characters. Append() does not do so.

Sean Vieira
jmein
@jmein - that's why I suggest specifying "text" in his get command, since, as he said, he's not url or html encoding his ajax data.
Sean Vieira
Its not what he is returning thats not working (or at least it does not sound like it). It is when he is setting the link path using the .html()
jmein
jmein
Thanks for the comment! I've updated my suggestion.
Sean Vieira
Thank you for all your answers, but append seems to do the same thing
Belgurinn
A: 

You should try using the $.ajax() function instead of $.get().

With $.ajax() you can specify your datatype yourself (and use other options).

Try something like this:

$.ajax({
type: "GET",
url: "/ajax.php",
data: "sec=search_users&ajax=1&search_for="+$(this).val(),
cache: false,
dataType: "html",
success: function(html){
 $(".rBoxContentStaff").html(html);
} });
DaMayan
Belgurinn
If html() is the problem then wouldnt $("#test").html("test
DaMayan
jmein