JQuery Mobile, Cordova的跨域CORS請求

來源:互聯網
上載者:User

Cordova手機app中,大量使用JQuery Mobile, datatjs(OData)等js架構,他們都遵循W3C HTTP CORS標準,app發起HTTP GET/POST等請求外網資源的時候,JQuery會偷偷摸摸的先發個HTTP OPTIONS請求外網資源,外網伺服器准許之後,JQuery才會正式的再發個HTTP GET/POST請求。
舉例如下:

OPTIONS /odata/applications/latest/app1/Connections HTTP/1.1Host: smp.springworks.info:8081Connection: keep-aliveAccess-Control-Request-Method: POSTOrigin: file://User-Agent: Mozilla/5.0 (Linux; Android 5.0.2; Redmi Note 3 Build/LRX22G; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/45.0.2454.95 Mobile Safari/537.36Access-Control-Request-Headers: accept, authorization, content-type, dataserviceversion, maxdataserviceversionAccept: */*Accept-Encoding: gzip, deflateAccept-Language: zh-CN,en-US;q=0.8X-Requested-With: com.mycompany.logonHTTP/1.1 200 OKAccess-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: file://Access-Control-Allow-Methods: POSTAccess-Control-Allow-Headers: accept, authorization, content-type, dataserviceversion, maxdataserviceversionAccess-Control-Max-Age: 3600Content-Length: 0Date: Wed, 19 Oct 2016 21:54:36 GMTServer: SAPPOST /odata/applications/latest/app1/Connections HTTP/1.1Host: smp.springworks.info:8081Connection: keep-aliveContent-Length: 2Accept: application/jsonMaxDataServiceVersion: 3.0Origin: file://User-Agent: Mozilla/5.0 (Linux; Android 5.0.2; Redmi Note 3 Build/LRX22G; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/45.0.2454.95 Mobile Safari/537.36DataServiceVersion: 1.0Authorization: Basic dG9tY2F0OnRvbWNhdA==Content-Type: application/jsonAccept-Encoding: gzip, deflateAccept-Language: zh-CN,en-US;q=0.8X-Requested-With: com.mycompany.logonHTTP/1.1 201 CreatedAccess-Control-Allow-Credentials: trueSet-Cookie: X-SMP-SESSIDSSO=B984633BCF2344F81A6F784362782724; Path=/; HttpOnlySet-Cookie: X-SMP-SESSID=29B2310D0600349CCBA294F7D7B851340121DBD6BD9A523EF08F1552CF25D566; Path=/; HttpOnlySet-Cookie: X-SUP-APPCID=41fc7dba-7c5e-4577-9184-12b95e89536e; Expires=Tue, 14-Oct-2036 21:54:36 GMT; Path=/Set-Cookie: X-SMP-APPCID=41fc7dba-7c5e-4577-9184-12b95e89536e; Expires=Tue, 14-Oct-2036 21:54:36 GMT; Path=/DataServiceVersion: 1.0Date: Wed, 19 Oct 2016 21:54:36 GMTLocation: http://smp.springworks.info:8081/odata/applications/latest/app1/Connections('41fc7dba-7c5e-4577-9184-12b95e89536e')Content-Type: application/json;charset=utf-8Transfer-Encoding: chunkedContent-Encoding: gzipVary: Accept-EncodingServer: SAP

參考:
http://demos.jquerymobile.com/1.3.2/faq/how-configure-phonegap-cordova.html
節選:The initial application document is loaded by the PhoneGap application by a local file:// URL. This means that if you want to pull in pages from your company’s remote server (phone home) you will have to refer to them with absolute URLs to your server. Because your document originates from a file:// URL, loading pages or assets from your remote server is considered a cross-domain request that can be blocked in certain scenarios.

W3C HTTP CORS標準](https://www.w3.org/TR/cors/)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.