2017-10-11 7 views
0

私はJavaScriptの世界ではかなり新しいです。だから私を抱きしめてください。 :-)xmlをエスケープ文字なしで取得する方法は? (javascript、angular)

ユーザー押しボタン(つまり、ユーザーのPCにxmlファイルをダウンロードします)、 "顧客注文を保存":

orders.component.html:

... 
    <form (ngSubmit)="saveOrdersAsXml()" novalidate> 
... 
    <div class="col-sm-12 text-right"> 
     <button type="submit" class="btn btn-primary btn-lg" >Save Customer Orders</button> 
    </div> 
... 

問題がある:どのように

export class OrderComponent implements OnInit 
{ 
    ... 

    private saveOrdersAsXml($event) 
    { 
     this.orderService.GetOrdersAsXml(this.customerNumber) 
      .subscribe(res => 
      { 
       // res = "Response with status: 200 OK for URL: http://localhost:52511/..." 
       // res._body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>..."  
       // _body contains the xml. How do I get the xml without escape characters?? 

       let xml: string = res; 
       const file = new Blob([xml], { type: 'text/xml;charset=utf-8' }); 
       saveAs(file, this.customerNumber + '.xml'); // Currently the file will contain "Response with status: 200 OK for URL: http://localhost:52511/..." - I need it to be xml. 
      }, error => this.errorMessage = <any>error); 
    } 
} 

orders.s:エスケープ文字?:

orders.component.tsなく、XMLを取得ervice.ts:

@Injectable() 
export class OrderService 
{ 
    ... 

    public GetOrdersAsXml(customerNumber: string): Observable<any> 
    { 
     const dataResult = this._http.get("http://localhost:52511/api/orders/getordersasxml/" + customerNumber, , this.authenticationService.jwt())) 
      .map((response: Response) => response) 
      .catch(this.handleError); 
     return dataResult; 
    } 
} 

OrdersController.cs:

[Produces("application/json")] 
[Route("api/orders")] 
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] 
public class OrdersController : Controller 
{ 
    ... 

    [HttpGet("getordersasxml/{customerNumber}")] 
    public string GetOrdersAsXml(string customerNumber) 
    { 
     // Here the xml is fine (e.g.: "<?xml version="1.0" encoding="UTF-8"?>...") 
     return _orderBL.GetOrdersAsXml(customerNumber); 
    } 
} 

EDIT:仕事の 後5時間私はまだそれを得ることはありません。

JObject、HttpResponseMessage、byte []とxDocumentを返そうとしました。

とフロントエンドで、私はこれを行う:私はこのようなxDocumentを使用する場合それは...

をイライラ取得している

private saveOrdersAsXml($event) 
{ 
    this.orderService.GetOrdersAsXml(this.customerNumber) 
     .subscribe(res => 
     { 
      console.log("1: " + xmlResult); 
      console.log("2: " + xmlResult._body); 
      console.log("3: " + new XMLSerializer().serializeToString(xmlResult._body)); 

      ... 
     }, error => this.errorMessage = <any>error); 
} 

コンソールに戻りますが、この:

1:ステータスの返答:URLの場合は200 OK ...

2:{"?@ version": "1.0"、 "@ encoding": "utf-16"} "Customer" ...

ERROR TypeError: 'XMLSerializer'で 'serializeToString'を実行できませんでした。パラメータ1の型が 'Node'ではありません。 SafeSubscriber.next(Subscriber.ts:204)の (SafeSubscriber.searchService.GetXMLFile.subscribe._this.errorMessage [as _next](license.component.ts:111) 、SafeSubscriber .__ tryOrUnsub(Subscriber.ts:254) Subscriber.nextで (Subscriber.ts:95)CatchSubscriber.Subscriber._nextで (Subscriber.ts:135)CatchSubscriber.Subscriber.next(Subscriber.tsで :Subscriber._next(135 Subscriber.ts)で 。 95)XMLHttpRequest.onLoadで (http.umd.js:1259)ZoneDelegate.invokeTaskで (zone.js:425)Object.onInvokeTaskで (core.umd.js:3913)

なぜそれがそうですやりたいバックエンドからフロントエンドにxml文字列を送信するような簡単な作業は何ですか?

私は現在歯科医に行くつもりですが、後で戻ってきます。どんな助けもありがとうございます。

+0

なぜあなたはサーバー側のコードに 'string'を返していますか?あなたができることを意味しますが、 'XDocument'を返すこともできます。それは役に立つドキュメントを検証するでしょう。また、 '.map((response:Response)=> response)'は何もしません。 '.map(response => response.blob())'を試したことがありますか? –

+0

'let xml:string = res._body'おそらく? 'res._body'にXMLが含まれていることを確認すると、 –

+0

@ JaromandaXこれは_bodyがエスケープされているという問題です。 – mith7

答えて

0

最後にそれが機能しました。

OrdersController.cs:

[HttpGet("getordersasxml/{customerNumber}")] 
public string GetOrdersAsXml(string customerNumber) 
{ 
    //return _orderBL.GetOrdersAsXml(customerNumber); // Gives trouble! 

    // Put xml as a byte array into a json 
    var xml = _orderBL.GetOrdersAsXml(customerNumber); 
    var response = JsonConvert.SerializeObject(new 
    { 
     data = Encoding.UTF8.GetBytes(xml) 
    }); 
    return response; 
} 

orders.component.ts:

private saveOrdersAsXml($event) 
{ 
    this.orderService.GetOrdersAsXml(this.customerNumber) 
     .subscribe(res => 
     { 
      // let xml: string = res; // Won't work 

      // Parse json-string to json object and convert the data byte array to a string. 
      let xml:string = atob(JSON.parse(res).data); 

      const file = new Blob([xml], { type: 'text/xml;charset=utf-8' }); 
      saveAs(file, this.customerNumber + '.xml'); 
     }, error => this.errorMessage = <any>error); 
} 

は、誰かがこれを見つけたホープはどうやら歯医者への旅は、私がこれをしなかった;-)

を助けました役に立った:-)

関連する問題