JQuery html () and & amp;

I am doing a search on a list of people. And I want him to show the results "on the fly", and so it is. 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 does not work. This is similar to html () as well as append ().

I did not find a solution for this, so I had to change the launch link.

+4
source share
5 answers

You said you were trying to set the correct link path?
If yes, try this

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

the .html() changes to both html and ( & ), but using .attr("href") sets the link path, and it also works with & s

+2
source

Instead of this:

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

Try the following:

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

text () escapes html characters ... I can't find anything about html () escaping characters (and, indeed, its documentation seems to point to something else.
However, after testing with a warning in jmein suggestion, it encodes special characters. Append () does not do this.

+2
source

html special characters when ajax request are not interpreted by the browser .. why the links came out that way. One thing you can do is replace sstring in javascript:

 data = str.replace(/&/, "&"); 
+1
source

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

With $ .ajax (), you can specify your data type yourself (and use other parameters).

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); } }); 
0
source

And also a way to convert receiving only text ( <a /> is only an assistant):

 $('<a /'>).html(data).text() 

This will move &amp; in & , as well as all other special characters, such as &imp; etc.

0
source

All Articles