您的位置:首页 > Web前端 > JQuery

JQuery Mobile, Cordova的跨域CORS请求

2016-10-20 06:42 274 查看
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.1
Host: smp.springworks.info:8081
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: 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.36
Access-Control-Request-Headers: accept, authorization, content-type, dataserviceversion, maxdataserviceversion
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,en-US;q=0.8
X-Requested-With: com.mycompany.logon

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: file://
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: accept, authorization, content-type, dataserviceversion, maxdataserviceversion
Access-Control-Max-Age: 3600
Content-Length: 0
Date: Wed, 19 Oct 2016 21:54:36 GMT
Server: SAP

POST /odata/applications/latest/app1/Connections HTTP/1.1
Host: smp.springworks.info:8081
Connection: keep-alive
Content-Length: 2
Accept: application/json
MaxDataServiceVersion: 3.0
Origin: 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.36
DataServiceVersion: 1.0
Authorization: Basic dG9tY2F0OnRvbWNhdA==
Content-Type: application/json
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,en-US;q=0.8
X-Requested-With: com.mycompany.logon

HTTP/1.1 201 Created
Access-Control-Allow-Credentials: true
Set-Cookie: X-SMP-SESSIDSSO=B984633BCF2344F81A6F784362782724; Path=/; HttpOnly
Set-Cookie: X-SMP-SESSID=29B2310D0600349CCBA294F7D7B851340121DBD6BD9A523EF08F1552CF25D566; Path=/; HttpOnly
Set-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.0
Date: Wed, 19 Oct 2016 21:54:36 GMT
Location: http://smp.springworks.info:8081/odata/applications/latest/app1/Connections('41fc7dba-7c5e-4577-9184-12b95e89536e') Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Content-Encoding: gzip
Vary: Accept-Encoding
Server: 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/)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: