开发者

ascii characters in my string

开发者 https://www.devze.com 2023-02-28 13:58 出处:网络
I have a string that I\'m trying to send over to my server code. I am getting a lot of these characters in the data - �. Do I need to serialize the data or what? I\'m using utf-8 encoding but that do

I have a string that I'm trying to send over to my server code. I am getting a lot of these characters in the data - . Do I need to serialize the data or what? I'm using utf-8 encoding but that doesn't fix it. I think that something is happening to my data on the JQuery side. How do I fix this?

Here is what my JQ looks like.

var id = $('#id').val();
var dataString = 'id='+ id;
$.ajax({
  type: "POST",
  url: "http...",
  data: dataString,
  dataType: 'json',
  success: function(data) {
  ...

Here is a sample string: `%ca 32d?

This string then becomes �32d? with UTF-8 and Ê32d?with iso-8859-1


EDIT: I noticed that this encoding issue only happens when the string begins with %ca. All oth开发者_运维问答ers seem to work as expected.


%xx in HTTP protocol is an encoded character, which the server will decode for you, even if you don't want to. You should either url encode your string before sending or replace every % with %25, which is the encoded equivalent of %. It's the same concept as with the \ characters. If you have \n in a string the engine will interpret it as a newline, even if n is part of a word for example.

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消