0

使用 olingo oData 库使用 oData V4 的 Javascript 应用程序。从浏览器的地址栏中输入:http://services.odata.org/v4/Northwind/Northwind.svc/Customers/ 返回 json 就好了... 这是 Fiddler 的标头 .. 请求标头

 GET /v4/Northwind/Northwind.svc/ HTTP/1.1
    Host: services.odata.org
    Connection: keep-alive
    Accept:     text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36
    DNT: 1
    Accept-Encoding: gzip, deflate, sdch
    Accept-Language: en-US,en;q=0.8
    Cookie: _ga=GA1.2.708581054.1436795740;   ARRAffinity=870cb949c1993c275a53bfec11e8eb1d159f40c7499f7f9453d895f29b9057a1

回复标题

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 3097
Content-Type: application/xml;charset=utf-8
Expires: Wed, 22 Jul 2015 19:43:25 GMT
Vary: *
Server: Microsoft-IIS/8.0
X-Content-Type-Options: nosniff
OData-Version: 4.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: Accept, Origin, Content-Type, MaxDataServiceVersion
Access-Control-Expose-Headers: DataServiceVersion
Date: Wed, 22 Jul 2015 19:42:24 GMT

**问题来自我使用完全相同的 uri olingo oDatajs 拨打电话的 javascript,但它在 Preflight 中失败且未实现 **

OPTIONS /V4/Northwind/Northwind.svc/Customers HTTP/1.1
Accept: */*
Origin: http://localhost:3238
Access-Control-Request-Method: GET
Access-Control-Request-Headers: accept, odata-maxversion
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: services.odata.org
Content-Length: 0
DNT: 1
Connection: Keep-Alive
Pragma: no-cache

响应头

HTTP/1.1 501 Not Implemented
Cache-Control: private
Content-Length: 49
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8
Expires: Wed, 22 Jul 2015 19:35:15 GMT
Vary: *
Server: Microsoft-IIS/8.0
X-Content-Type-Options: nosniff
OData-Version: 4.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: Accept, Origin, Content-Type, MaxDataServiceVersion
Access-Control-Expose-Headers: DataServiceVersion
Set-Cookie: ARRAffinity=870cb949c1993c275a53bfec11e8eb1d159f40c7499f7f9453d895f29b9057a1;Path=/;Domain=services.odata.org
Date: Wed, 22 Jul 2015 19:34:14 GMT

我必须假设它在请求标头中,因为失败是在返回响应到达浏览器之前。我在本地服务器上有一个 oData V4 端点,并且即使我可以从地址栏中处理相同的 URI,我也能够通过 CORS 只让它返回 404。

该请求的地址栏标题

GET /odata/Addresses?$top=10 HTTP/1.1
Host: localhost:43076
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

回复

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
DataServiceVersion: 3.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B? TjpcQXNwUHJvamVjdHNcRmxleENvcmVcRmxleENvcmVcb2RhdGFcQWRkcmVzc2Vz?=
X-Powered-By: ASP.NET
Date: Wed, 22 Jul 2015 18:44:05 GMT
Content-Length: 23418

还有这个 javascript 中的失败标头... 请求

GET /odata/Addresses?$top=10 HTTP/1.1
OData-MaxVersion: 4.0
Accept: application/json;q=0.9, */*;q=0.1
Referer: http://localhost:3238/
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: localhost:43076
DNT: 1
Connection: Keep-Alive

回复

HTTP/1.1 404 Not Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?    TjpcQXNwUHJvamVjdHNcRmxleENvcmVcRmxleENvcmVcb2RhdGFcQWRkcmVzc2Vz?=
X-Powered-By: ASP.NET
Date: Wed, 22 Jul 2015 18:44:35 GMT
Content-Length: 3186

正如您所看到的,Host 和 GET 在这两种情况下都是相同的,所以我不明白为什么我在 javascript 请求中得到 404。有谁比我更了解这些标题吗?

更新 我已经确定问题的后半部分(命中我的本地服务器)的问题是请求标头中的“OData-MaxVersion:4.0”。ASP.Net odata 本地服务器正在运行 oData 版本 3。一旦我将后端重新设计为 oData v4,它就可以很好地返回有效负载。我通过在提琴手中一一删除请求标头值直到它起作用来确定问题。

对我来说问题较小但仍未解决的是 Northwind oData 在预检时返回 501 这里是调用 oDatajs-4.0.0 函数的 javascript 函数

var headers = { "Content-Type": "application/json", Accept: "application/json,text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" };
                var request = {
                    requestUri: "http://services.odata.org/V4/Northwind/Northwind.svc/Customers",
                    method: "GET",
                    headers: headers,
                    data: null
                };


                odatajs.oData.request(request, function (data, response) {
                    if ((response.statusCode == '200') &&
                        (response.body.indexOf('}', response.body.length - 1) == response.body.length - 1) &&
                        response.headers['Content-Type'] == "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8") {
                        document.getElementById('msg').innerHTML += ("<div>odatajs V4 testing pass!</div>");
                    } else {
                        document.getElementById('msg').innerHTML += ("<div>odatajs V4 testing failed.</div>");
                    }
                }, function (err) {
                    document.getElementById('msg').innerHTML += ("<div>odatajs V4 testing failed.</div>");
                });
4

0 回答 0