請幫忙。我正在嘗試學習 REST 概念并使用 Delphi 和 REST 物件制作第一個程式。我遇到了一個我不知道如何解決的問題。在資料庫中,我使用母語(波斯尼亞語)中的特殊字符進行了測驗:?、?、?、?、?。當我通過 GET 方法傳遞此文本時,在表單上的物件中決議和顯示此文本的物件將這些字符顯示為“?”、“?”、“?è”等。我研究并嘗試解決問題,但沒有成功。我嘗試使用 Utf8ToAnsi 函式并在 RESTClient、RESTRequest 和 RESTResponce 物件引數中使用 iso-8859-2 而不是 UTF-8。請尋求有關如何解決或調查此問題的幫助、說明或建議。
服務器端的源代碼:
procedure TWebModule1.UsersGet(Request: TWebRequest; Response: TWebResponse);
var
a: TJSONArray;
o: TJSONObject;
i: Integer;
Q1: TADOQuery;
begin
Q1:=TADOQuery.Create(nil);
Q1.Connection:= BasicDBConn.Konekcija;
with Q1 do
begin
Active:=False;
SQL.Clear;
SQL.Add('Select USER_ID, USER_NAME, ROLE_NAME from USERS, ROLES '
' where USERS.ROLE_ID=ROLES.ROLE_ID ');
Active:= True;
end;
a := TJSONArray.Create;
if Q1.RecordCount > 0 then
begin
for i:=1 to Q1.RecordCount do
begin
o := TJSONObject.Create;
o.AddPair('USER_ID', Q1.Fields.Fields[0].Value);
o.AddPair('USER_NAME', Q1.Fields.Fields[1].Value);
o.AddPair('ROLE_NAME', Q1.Fields.Fields[2].Value);
Q1.Next;
a.AddElement(o);
end;
end;
Response.ContentType := 'application/json';
Response.Content := a.ToString;
a.DisposeOf;
Q1.Free;
end;
在客戶端:
procedure TGlavna.Button1Click(Sender: TObject);
begin
RESTRequest.Resource := 'Users';
RESTRequest.Method := TRESTRequestMethod.rmGet;
RESTRequest.Response := RESTResponse;
RESTRequest.Execute;
if assigned(fJSONArray) then fJSONArray.DisposeOf;
fJSONArray := TJSONObject.ParseJSONValue(RESTResponse.Content) as TJSONArray;
if RESTResponse.Content.IsEmpty then Application.MessageBox('Empty', 'Information', MB_OK)
else Memo1.Lines.Add(RESTResponse.Content);
end;
這是 DB 中的資料的樣子
USER_ID USER_NAME ROLE_ID u1 測驗用戶 2
u2 T_?_?_?_?_? 1
u3 t_?_?_?_?_? 1
u4 Bradi? Kenan 1
在網路瀏覽器中,回應資料為:
[{"USER_ID":"u1","USER_NAME":"測驗用戶","ROLE_ID":"2"},{"USER_ID":"u3","USER_NAME":"t_è_?_?_D_?","ROLE_ID":" 1"},{"USER_ID":"u4","USER_NAME":"Bradi? Kenan","ROLE_ID":"1"},{"USER_ID":"u2","USER_NAME":"T_è_?_?_e_?","ROLE_ID ":"1"}]
Headers in web browser:
General:
Request URL: http://localhost:8080/Users
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:8080
Referrer Policy: strict-origin-when-cross-origin
Response Headers:
Connection: close
Content-Length: 228
Content-Type: application/json; charset=ISO-8859-1
Date: Mon, 27 Dec 2021 17:07:54 GMT
Request Headers:
Accept: text/html,application/xhtml xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Host: localhost:8080
sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36
謝謝...
uj5u.com熱心網友回復:
HTTP 回應包含以下行:
Content-Type: application/json; charset=ISO-8859-1
因此客戶端處理將回應內容解釋為 ISO-85591 編碼而不是 UTF-8。您的服務器端代碼包含以下行:
Response.ContentType := 'application/json';
您的服務器端代碼應該清除 charset=ISO-85591 部分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/395844.html
標籤:休息 德尔福 特殊字符 delphi-xe7
