I need to pass fields content from a table database to a JavaSricpt function in my web page.
I found an error when I have a backslash in the string :
{"DT_RowId":"4427","pront":"4427","nome":"JOHN DOE","conv":"PETROBRAS\ DISTR ( R)","cpf":"","email":""}
See:"PETROBRAS\"
In this case, I found that backslash was the cause of the problem.
I know I can solve this particular problem, but the table database I am using has many rows, and may be, can have others chars that can cause problems to JavaScript in my page
Is there a delphi function to escape all invalids chars(considering JavaScript) in a delphi string in order to pass to jasvascript function?
function EscapeDelphiStringToJS(s:string):string
begin
result:= "how could I do the escaping?"
end;
Remy, Here is the code I am using to create the Json. I am using XE8.
In order to return the data to my ajax function, I just convert the result function to string using: jso.tostring; But, this is not the problem.
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;
This is the result of the function:
{
"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": ""
}]
}