ODataModel错误400,$元素在调用时附加到url

时间:2016-02-29 09:01:05

标签: javascript xmlhttprequest odata sapui5

我希望通过提出此类请求来接收信息(联系人列表):

var oModel = new sap.ui.model.odata.ODataModel(
  "http://myserver.abc.com:8010/sap/opu/odata/sap/contact_person_srv/ContactPerson"
);
console.log(oModel);

如果我通过浏览器询问此地址,一切正常:

Request Method:GET
Status Code:200 OK
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,en-US;q=0.6,en;q=0.4,uk;q=0.2
Cache-Control:max-age=0
Connection:keep-alive
Cookie:sap-contextid=SID%3aANON%3asyb-nr8_NR8_10%3akz_pnaJCXLoloQuOmUyvxNcb9BVY8nZMBEzdU43u-NEW; SAPWP_active=1; sap-login-XSRF_NR8=20160225162649-bzlhG6X3gJoHL7alyJfmiQ%3d%3d; sap-usercontext=sap-language=RU&sap-client=100; SAP_SESSIONID_NR8_100=vfnkrJWROAaG0TLwlhWVE5FPeNLb3BHlhF4ADCnEUZs%3d
Host:myserver.abc.com:8010
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36

但是,从我的控制器的请求采取以下形式:

Request URL:http://myserver.abc.com:8010/sap/opu/odata/sap/contact_person_srv/ContactPerson/$metadata
Request Method:OPTIONS

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,en-US;q=0.6,en;q=0.4,uk;q=0.2
Access-Control-Request-Headers:accept, accept-language, maxdataserviceversion
Access-Control-Request-Method:GET
Connection:keep-alive
Host:myserver.abc.com:8010
Origin:http://localhost:63342
Referer:http://localhost:63342/test/webapp/index.html
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36

为什么要求方法 - OPTIONS

如何制作GET

为什么它会添加到请求字符串/$metadata

服务器发送响应请求的xml消息,如何获取数据以产生普通的javascript-object?

如果我这样做的话:

    $.ajax({
        type: 'GET',
        url: 'http://myserver.abc.com:8010/sap/opu/odata/sap/contact_person_srv/ContactPerson',
        success: function(data){
            console.log(data)
        }
    });

一切正常!

我在截图中附上了服务器的回复,帮助我理解。

enter image description here

1 个答案:

答案 0 :(得分:0)

首先需要$metadata部分,以便了解所期望的类型,请参阅http://www.odata.org/documentation/odata-version-2-0/overview/第3和第5段。

对于HTTP OPTIONS请求,这很可能与CORS有关,并且请求预先通过了OPTIONS请求。