2017-01-27 6 views
0

テーブルのデータベースからフィールドのコンテンツをWebページのJavaSricpt関数に渡す必要があります。デルファイ文字列をJavaScript関数にエスケープする

{"DT_RowId":"4427","pront":"4427","nome":"JOHN DOE","conv":"PETROBRAS\ DISTR (R)","cpf":"","email":""} 

を参照してください:私は、文字列内のバックスラッシュがある場合、私はエラーを検出しました

この場合、「ペトロブラス\」

を、私は、バックスラッシュが問題の原因であることがわかりました。

私は、この特定の問題を解決することができます知っているが、私は使用していますテーブルデータベースは、多くの行を持っている、とすることができ、私のページではJavaScriptを

を問題を引き起こす可能性があり、他の文字を持つことができますが、Delphiの機能がありますjasvascript関数に渡すためにdelphi文字列内のすべての無効な文字(JavaScriptを考慮)をエスケープするには?

function EscapeDelphiStringToJS(s:string):string 
begin 
    result:= "how could I do the escaping?" 
end; 

レミー、私は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

JavaScriptに渡すデータはJSONです。あなたはデータベースデータをJSONに変換するために何を使用していますか?スラッシュ文字をエスケープする必要がありますが、それはあなたのケースでそうしていません。手動でデータを変換していますか?その場合は、代わりにJSONライブラリを使用する必要があります。 –

+0

意図した性質のJSONライブラリを使用します。 –

+0

また、ここで見ることができます:[convert-a-string-to-and-json](http://stackoverflow.com/questions/11785963/how-do-i-convert-a-string-to -and-from-json-with-escaped-special-characters-using) –

答えて

-2

のsvn https://svn.code.sf.net/p/alcinoe/code/からは(あなたはまた、Unicode文字列の実装内で見つけることができますが、私は参照としてここにAnsiStringのを与えたためにprefere)

{******************************************************************************************} 
// 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; 
関連する問題