我在JavaScript世界中很新。所以請忍受我。 :-)如何獲得沒有轉義字符的xml? (javascript,angular)
用戶按下按鈕 「保存客戶訂單」(這將下載一個XML文件到用戶PC):
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>
...
的問題是:如何獲取XML WITHOUT轉義字符?:
orders.component.ts:
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 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);
}
}
編輯: 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:狀態響應:200 OK for URL ...
2:{「?xml」:{「@ version」:「1.0」,「@ encoding」:「utf-16」} ,「客戶」...
錯誤類型錯誤:未能在'XMLSerializer'上執行'serializeToString':參數1不是'Node'類型。在SafeSubscriber.next(Subscriber.ts:204)上的SafeSubscriber .__ tryOrUnsub(Subscriber.ts:254) 處的SafeSubscriber.searchService.GetXMLFile.subscribe._this.errorMessage [作爲_next](license.component.ts:111) 處的 。 在Subscriber._next(Subscriber.ts:135) 在Subscriber.next(Subscriber.ts:95) 在CatchSubscriber.Subscriber._next(Subscriber.ts:135) 在CatchSubscriber.Subscriber.next(Subscriber.ts: 95) 在XMLHttpRequest.onLoad(http.umd.js:1259) 在ZoneDelegate.invokeTask(zone.js:425) 在Object.onInvokeTask(core.umd.js:3913)
爲何如此不用擔心這樣一個簡單的任務就像從後端向前端發送xml字符串一樣?
我現在要去看牙醫,但稍後會回來。任何幫助表示讚賞。
你爲什麼要在你的服務器端代碼中返回一個'string'?我的意思是你可以,但你也可以返回'XDocument'。這將驗證有用的文檔。另外,'.map((response:Response)=> response)'什麼也不做。你有沒有嘗試'.map(response => response.blob())'? –
'let xml:string = res._body'也許?看到你說'res._body'包含XML –
@JaromandaX這樣做的問題是_body被轉義。 – mith7