日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用 SAP UI5 消费 OData 服务的一些常见错误和解决方案

發(fā)布時間:2023/12/19 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用 SAP UI5 消费 OData 服务的一些常见错误和解决方案 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

錯誤消息1

Access to XMLHttpRequest at ‘http://localhost:8081/https://services.odata.org/V2/Northwind/Northwind.svc/$metadata?sap-language=EN’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Request header field maxdataserviceversion is not allowed by Access-Control-Allow-Headers in preflight response.

原因是 maxdataserviceversion 這個和 OData 服務(wù)版本相關(guān)的字段,沒有出現(xiàn)在服務(wù)器端配置的 Access-Control-Allow-Headers 數(shù)組里,因此引起了跨域 CORS error.

maxdataserviceversion 這個字段在 Chrome 開發(fā)者工具 network 標(biāo)簽頁里能看到:

解決方案:

app.all('*', function(req, res, next) {res.header("Access-Control-Allow-Origin", "*");res.header("Access-Control-Allow-Headers", "*");res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");res.header("X-Powered-By",' 9.0.0');next();});

將 Access-Control-Allow-Headers 的值改成 * 即可。錯誤消失:

錯誤2

下面 url 對應(yīng)的 metadata,無法在瀏覽器里正常顯示。期望的結(jié)果是,地址欄里輸入 url 之后,我們能看到 http://localhost:8081/ 后面的 metadata 實際的內(nèi)容,以 xml 格式正常顯示:

http://localhost:8081/https://services.odata.org/V2/Northwind/Northwind.svc/$metadata?sap-language=EN

正常情況下,請求的 header 字段是:
Accept: application/xml

根據(jù) express response 對象的 API 說明,如果 send 方法傳入的參數(shù)是一個字符串,則響應(yīng)結(jié)構(gòu)的 Content-Type 自動設(shè)置為text/html:

這一點可以在 Postman 里觀察到:

我們可以在代理服務(wù)器的實現(xiàn)里,使用 res.set 方法可以將 Content-Type 手動修改成 application/xml:

這樣,在瀏覽器里就能正常顯示, 通過代理服務(wù)器獲取的 xml 格式的 metadata 了:

錯誤消息3

The /$batch resource only supports POST method request

header 字段里包含了 boundary 值:

對于 $batch 操作來說,Accept 的值為 multipart/mixed:

確實就掛起了:

當(dāng)消費者想要執(zhí)行多個獨立的 HTTP 調(diào)用并希望避免多次服務(wù)器往返時,通常會使用 OData 批處理請求。

在 SAP UI5 里使用 OData Model 發(fā)送 batch 請求的示例代碼如下:

var tmpModel = new ODataModel("https://xxyyzz.com/sap/opu/odata/<your_service>_SRV/", true); tmpModel.setDefaultBindingMode(sap.ui.model.BindingMode.TwoWay); tmpModel.setUseBatch(true); this.getView().setModel(tmpModel, "tmpModel"); tmpModel.setDeferredGroups(["foo"]); var mParameters = {groupId:"foo",success:function(odata, resp){ console.log(resp); },error: function(odata, resp) { console.log(resp); }};for(var m=0; m<oPayload.length; m++) {tmpModel.update("/YOUR_ENTITYSet(Key1='Valu1',Key2='Value2')", oPayload[m], mParameters); } tmpModel.submitChanges(mParameters);

更多Jerry的原創(chuàng)文章,盡在:“汪子熙”:

總結(jié)

以上是生活随笔為你收集整理的使用 SAP UI5 消费 OData 服务的一些常见错误和解决方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。