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/)