Cannot send special characters through jQuery ajax

I am developing a web page where a user searches for files using tags. I am using jQuery Ajax to make a remote API call (database). Everything works fine when I use non-specific characters like az, but it doesn't work, for example using åäö.

On the server I use PHP. I print the tag to see if it’s coming, and all az work fine, but åäö does not appear at all. They do not seem to "come."

What could be wrong?

This is my jQuery code:

var tags = $('#tags').val(); $.ajax ({ type: "POST", url: base_url + "search", data: "tags=" + tags + "&limit=" + limit, beforeSend: function (html) { $("#search_results").html("Searching for files..."); }, success: function (html) { $("#search_results").html(html); }, error: function (html) { $("#search_results").html('Something went wrong!'); } }); 

This is my server side code:

 echo ($_POST['tags']); 

I searched and looked for related questions about this here on SO, but didn't help me, unfortunately.

UPDATE

Using this, I decided! Now it works fine.

 {tags: encodeURIComponent(tags), limit: limit} 
+6
source share
3 answers

Data (tags) must be encoded before sending to the server using encodeURIComponent ()

+11
source

Below code works fine for sending & and "" or any special characters via ajax call:

 specialChar1= "JΛ̊KE# 2@ #*&($^@%#*@#%))*$&@*(""" ; specialchar2 ="??&!!--##"; url = "/get/" + encodeURIComponent( specialChar1) +"/"+ encodeURIComponent ( specialchar2 ) 
+1
source

You can achieve this with a JSON object.

For instance:

 [{"AttributeId":"4035","Value":"Street & House"}] 

or, you can use URLencode before publishing.

0
source

Source: https://habr.com/ru/post/1390997/


All Articles