我需要将字段内容从表数据库传递到我的网页中的JavaSricpt函数。
我在字符串中有反斜杠时发现错误:
{"DT_RowId":"4427","pront":"4427","nome":"JOHN DOE","conv":"PETROBRAS\ DISTR ( R)","cpf":"","email":""}
请参阅: “PETROBRAS \”
在这种情况下,我发现反斜杠是导致问题的原因。
我知道我可以解决这个特殊问题,但是我使用的表数据库有很多行,可能会有其他字符可能会导致页面中的JavaScript出现问题
是否有一个delphi函数可以在delphi字符串中转义所有invalids字符(考虑JavaScript)以传递给jasvascript函数?
function EscapeDelphiStringToJS(s:string):string
begin
result:= "how could I do the escaping?"
end;
Remy,这是我用来创建Json的代码。我正在使用XE8。
为了将数据返回到我的ajax函数,我只需使用以下命令将结果函数转换为字符串:jso.tostring;但是,这不是问题。
function TContentClient.select_client(aParams: TStrings):
var so,jsoItem:TJsonObject;
jsa:TJsonArray;
jsp:TJSONPair;
ds:Tdataset;
.....
begin
TJsonObject;_recordsTotal:=IntToStr(ds.RecordCount);
jso:=TJsonObject.Create;
jso.AddPair('draw',TJsonNumber.Create(_draw));
jso.AddPair('recordsTotal',TJsonNumber.Create(_recordsTotal));
jso.AddPair('recordsFiltered',TJsonNumber.Create(_recordsTotal));
//create an json-array
jsa := TJsonArray.Create();
//add array to object
while not ds.eof do begin
//add items to the _first_ elemet of array
jsoItem := TJsonObject.Create();
//add object pairs
jsoItem.AddPair(TJsonPair.Create('DT_RowId',TJsonString.Create(ds.FieldByname('pront').AsString)));
jsoItem.AddPair(TJsonPair.Create('pront', TJsonString.Create(ds.FieldByname('pront').AsString)));
jsoItem.AddPair(TJsonPair.Create('nome', TJsonString.Create(ds.FieldByname('nome').AsString)));
jsoItem.AddPair(TJsonPair.Create('endereco', TJsonString.Create(ds.FieldByname('endereco').AsString)));
jsoItem.AddPair(TJsonPair.Create('bairro', TJsonString.Create(ds.FieldByname('bairro').AsString)));
jsoItem.AddPair(TJsonPair.Create('cidade', TJsonString.Create(ds.FieldByname('cidade').AsString)));
jsoItem.AddPair(TJsonPair.Create('estado', TJsonString.Create(ds.FieldByname('estado').AsString)));
jsoItem.AddPair(TJsonPair.Create('telefone', TJsonString.Create(ds.FieldByname('telres').AsString)));
jsoItem.AddPair(TJsonPair.Create('convenio', TJsonString.Create(ds.FieldByname('convenio').AsString)));
jsoItem.AddPair(TJsonPair.Create('cpf', TJsonString.Create(ds.FieldByname('cpf').AsString)));
jsoItem.AddPair(TJsonPair.Create('email', TJsonString.Create(ds.FieldByname('email').AsString)));
//put it into array
jsa.AddElement(jsoItem);
ds.Next;
end;
jsp := TJSONPair.Create('data', jsa);
jso.AddPair(jsp);
result:=jso;
end;
这是该功能的结果:
{
"draw": 3,
"recordsTotal": 5303,
"recordsFiltered": 5303,
"data": [{
"DT_RowId": "2582",
"pront": "2582",
"nome": "XXXXX",
"endereco": "RUA TEODORO DA SILVA,333/103",
"bairro": "VILA ISABEL",
"cidade": "RIO DE JANEIRO",
"estado": "RJ",
"telefone": "3683*2690",
"convenio": "GOLDEN CROSS ( B)",
"cpf": "",
"email": ""
}, {
"DT_RowId": "2818",
"pront": "2818",
"nome": "YYYYY",
"endereco": "RUA PEREIRA NUNES , 395 AP 308",
"bairro": "VILA ISABEL",
"cidade": "RIO DE JANEIRO",
"estado": "RJ",
"telefone": "YYYYY",
"convenio": "UN",
"cpf": "216820707-00",
"email": ""
}, {
"DT_RowId": "1015",
"pront": "1015",
"nome": "YYYYYY",
"endereco": "rua francisca zieze, 192",
"bairro": "aboliçao",
"cidade": "RIO DE JANEIRO",
"estado": "RJ",
"telefone": "25945909",
"convenio": "UN",
"cpf": "71883592704",
"email": ""
}, {
"DT_RowId": "1701",
"pront": "1701",
"nome": "JJJJJJ",
"endereco": "AV 28 DE SETEMBRO 163 APT 704",
"bairro": "VILA ISABEL",
"cidade": "RIO DE JANEIRO",
"estado": "RJ",
"telefone": "2253-7625",
"convenio": "PETROBRAS DISTR\ ( R)",
"cpf": "",
"email": ""
}, {
"DT_RowId": "1076",
"pront": "1076",
"nome": "LLLLLLLL",
"endereco": "RUA ARISTIDES ,241/304",
"bairro": "MEIER",
"cidade": "RIO DE JANEIRO",
"estado": "RJ",
"telefone": "2501*6600",
"convenio": "CORREIOS",
"cpf": "",
"email": ""
}, {
"DT_RowId": "5959",
"pront": "5959",
"nome": "ZULEICA IIIII",
"endereco": "rua dos araujos 11a, bl 02 ap 301",
"bairro": "tijuca",
"cidade": "RIO DE JANEIRO",
"estado": "RJ",
"telefone": "2254-5682",
"convenio": "AMIL",
"cpf": "53150457734",
"email": ""
}, {
"DT_RowId": "5894",
"pront": "5894",
"nome": "ZULEICA AAAAA",
"endereco": "RUA PROF GABIZO,332/102",
"bairro": "TIJUCA",
"cidade": "RIO DE JANEIRO",
"estado": "RJ",
"telefone": "2568*9654",
"convenio": "UNIMED (SCOFANO)",
"cpf": "54016843715",
"email": ""
}, {
"DT_RowId": "4034",
"pront": "4034",
"nome": "ZILENE KAKAKA",
"endereco": "RUA AFONSO FERREIRA ,162",
"bairro": "ENGENHO DE DENTRO",
"cidade": "RIO DE JANEIRO",
"estado": "RJ",
"telefone": "2597*2352",
"convenio": "CORREIOS",
"cpf": "",
"email": ""
}, {
"DT_RowId": "5126",
"pront": "5126",
"nome": "ZILDA MAMAMA",
"endereco": "AV DOM HELDER CAMARA 1201",
"bairro": "BENFICA",
"cidade": "RIO DE JANEIRO",
"estado": "RJ",
"telefone": "7646-0691",
"convenio": "BRADESCO (B)",
"cpf": "",
"email": ""
}, {
"DT_RowId": "4497",
"pront": "4497",
"nome": "ZILDA LALALA",
"endereco": "RUA SABARRETO , 13 CASA",
"bairro": "FONSECA - NITEROI",
"cidade": "RIO DE JANEIRO",
"estado": "RJ",
"telefone": "2721-6646",
"convenio": "UNIMED (SCOFANO)",
"cpf": "",
"email": ""
}]
}
答案 0 :(得分:-2)
来自svn https://svn.code.sf.net/p/alcinoe/code/(您也可以在unicode字符串实现中找到,但我更喜欢在这里给出ansiString作为参考)
{******************************************************************************************}
// https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Values,_variables,_and_literals
function ALJavascriptEncode(const Src: AnsiString; const useNumericReference: boolean = True): AnsiString;
var i, l: integer;
Buf, P: PAnsiChar;
ch: Integer;
begin
Result := '';
L := Length(src);
if L = 0 then exit;
if useNumericReference then GetMem(Buf, L * 6) // to be on the *very* safe side
else GetMem(Buf, L * 2); // to be on the *very* safe side
try
P := Buf;
for i := low(Src) to high(Src) do begin
ch := Ord(src[i]);
case ch of
8: begin // Backspace
if useNumericReference then begin
ALStrMove('\u0008', P, 6);
Inc(P, 6);
end
else begin
ALStrMove('\b', P, 2);
Inc(P, 2);
end;
end;
9: begin // Tab
if useNumericReference then begin
ALStrMove('\u0009', P, 6);
Inc(P, 6);
end
else begin
ALStrMove('\t', P, 2);
Inc(P, 2);
end;
end;
10: begin // New line
if useNumericReference then begin
ALStrMove('\u000A', P, 6);
Inc(P, 6);
end
else begin
ALStrMove('\n', P, 2);
Inc(P, 2);
end;
end;
11: begin // Vertical tab
if useNumericReference then begin
ALStrMove('\u000B', P, 6);
Inc(P, 6);
end
else begin
ALStrMove('\v', P, 2);
Inc(P, 2);
end;
end;
12: begin // Form feed
if useNumericReference then begin
ALStrMove('\u000C', P, 6);
Inc(P, 6);
end
else begin
ALStrMove('\f', P, 2);
Inc(P, 2);
end;
end;
13: begin // Carriage return
if useNumericReference then begin
ALStrMove('\u000D', P, 6);
Inc(P, 6);
end
else begin
ALStrMove('\r', P, 2);
Inc(P, 2);
end;
end;
34: begin // Double quote
if useNumericReference then begin
ALStrMove('\u0022', P, 6);
Inc(P, 6);
end
else begin
ALStrMove('\"', P, 2);
Inc(P, 2);
end;
end;
38: begin // & ... we need to encode it because in javascript ' or & will be converted to ' and error unterminated string
ALStrMove('\u0026', P, 6);
Inc(P, 6);
end;
39: begin // Apostrophe or single quote
if useNumericReference then begin
ALStrMove('\u0027', P, 6);
Inc(P, 6);
end
else begin
ALStrMove('\''', P, 2);
Inc(P, 2);
end;
end;
60: begin // < ... mostly to hide all </script> tag inside javascript.
// http://www.wwco.com/~wls/blog/2007/04/25/using-script-in-a-javascript-literal/
ALStrMove('\u003C', P, 6);
Inc(P, 6);
end;
62: begin // > ... mostly to hide all HTML tag inside javascript.
ALStrMove('\u003E', P, 6);
Inc(P, 6);
end;
92: begin // Backslash character (\).
if useNumericReference then begin
ALStrMove('\u005C', P, 6);
Inc(P, 6);
end
else begin
ALStrMove('\\', P, 2);
Inc(P, 2);
end;
end;
else Begin
P^:= AnsiChar(ch);
Inc(P);
end;
end;
end;
SetString(Result, Buf, P - Buf);
finally
FreeMem(Buf);
end;
end;