Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

odata - Problems with jaydata basic authentication

Jaydata & Javascript noobie. I'm having problems with jaydata and basic authentication, can't seem to get it to work. I specify user and password but the headers never contains an auth line. Jaydata version is 1.3.6

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="/scripts/jquery-1.11.0.js"></script>
    <script type="text/javascript" src="/scripts/datajs-1.0.3.js"></script>
    <script type="text/javascript" src="/scripts/jaydata.js"></script>
    <script type="text/javascript" src="/scripts/jaydataproviders/oDataProvider.js"></script>
    <script type="text/javascript" src="/scripts/zawtst.js"></script>
    <script>
        var nwcontext = new ZAWTST_SRV.ZAWTST_SRV({
            name: 'oData',
            oDataServiceHost: 'http://vmerpd01.sap.int:8080/sap/opu/odata/sap/zawtst_srv',
            user: "XXXXX",
            password: "xxxxxxx"
        });

        function createItemLI(name, id, css) {
            var li = $('<li></li>').append(name).addClass(css).data('id', id);
            return li;
        }

        $(function () {
            nwcontext.SalesOrderCollection.toArray(function (SalesOrderCollection) {
                SalesOrderCollection.forEach(function (category) {
                    $('#categories').append(
                        createItemLI(SalesOrder.SdDoc, SalesOrder.Material, 'category'));
                });
            });
        });
    </script>
</head>
<body>
    <ul id="categories">
    </ul>
</body>

When testing in Chrome, I see no auth line:

Remote Address:10.1.8.171:8080
Request URL:http://vmerpd01.sap.int:8080/sap/opu/odata/sap/zawtst_srv/SalesOrderCollection
Request Method:OPTIONS
Status Code:401 Unauthorized
Request Headers -----------------------
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, maxdataserviceversion, dataserviceversion, authorization
Access-Control-Request-Method:GET
Cache-Control:max-age=0
Connection:keep-alive
Host:vmerpd01.sap.int:8080
Origin:http://10.8.0.6:8080
Referer:http://10.8.0.6:8080/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
Response Headers --------------------
content-encoding:gzip
content-length:947
content-type:text/html; charset=utf-8
sap-system:GD1
www-authenticate:Basic realm="SAP NetWeaver Application Server [GD1/400]"

Yet here in the javascript console I can see the auth line.

Exception {name: "HTTP request failed", message: "", data: Arguments[1], stack: (...), _getStackTrace: function}
data: Arguments[1]
0: Object
message: "HTTP request failed"
request: Object
callbackParameterName: "$callback"
data: undefined
enableJsonpCallback: false
formatQueryString: "$format=json"
headers: Object
Accept: "application/atomsvc+xml;q=0.8, application/json;odata=verbose;q=0.5, */*;q=0.1"
Authorization: "Basic WFhYWFg6eHh4eHh4eA=="
DataServiceVersion: "2.0"
MaxDataServiceVersion: "2.0"
__proto__: Object

Any thoughts ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the $data.Authentication.BasicAuth API to login:

httpAuth = new $data.Authentication.BasicAuth.BasicAuth({
    username: "XXXXX",
    password: "xxxxxxx"
    });

context.storageProvider.AuthenticationProvider = httpAuth.CreateRequest({});

httpAuth.Login({
    success: function (data) { }
              });

References


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...