JayData oData request with custom headers

眉间皱痕 提交于 2019-12-11 08:47:33

问题


I need to send custom headers to my wcf oData Service but with the following function the headers dont get modified.

entities.onReady(function () {

    entities.prepareRequest = function(r) {
        r[0].headers['APIKey'] = 'ABC';
    };

    entities.DataServiceClient.toArray(function (cli) {
        cli.forEach(function (c) {
            console.log(c.Name)
        });
    });
});

headers are not affected. any clue?

thanks!


回答1:


It seems that the marked answer is incorrect. I was suffering from a similar issue, but got it working without changing datajs.

My issue was that I was doing a cross domain (CORS) request, but didn't explicitly allow the headers. After I added the correct CORS header to the webservice, it worked.




回答2:


EDIT

On second thought, it seems like there is still something broken in JayData for MERGE requests.

This is NOT CORS and has nothing to do with it!

see JayData oData request with custom headers - ROUND 2

the bellow "hack" works, but the above question should take this problem to a new level.

----------

Old answer

Nevermind I found a solution.

It seems like prepareRequest is broken in JayData 1.3.2 (ODataProvider).

As a hack, I added an extraHeaders object in the providerConfiguration (oDataProvider.js):

  this.providerConfiguration = $data.typeSystem.extend({
                   //Leave content unchanged and add the following:
                    extraHeaders: {}
                }, cfg);

then at line 865 modify requestData like this:

var requestData = [
                    {
                        requestUri: this.providerConfiguration.oDataServiceHost + sql.queryText,
                        method: sql.method,
                        data: sql.postData,
                        headers: _.extend({
                            MaxDataServiceVersion: this.providerConfiguration.maxDataServiceVersion
                        },this.providerConfiguration.extraHeaders)
                    },

NOTE: Iam using lodash for conveniance, any js extend should do the trick.

then you just create your client like this:

 var entities = new Entities.MyEntities({
            name: 'oData',
            oDataServiceHost: 'http://myhost.com/DataService.svc',
            maxDataServiceVersion: "2.0",
            //enableJSONP: true,
            extraHeaders: {apikey:'f05d1c1e-b1b9-5a2d-2f44-da811bd50bd5', Accept:'application/json;odata=verbose'}
        }
    );


来源:https://stackoverflow.com/questions/19242958/jaydata-odata-request-with-custom-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!