我正在使用zapworks工作室开发AR体验。它使用Z.ajax进行ajax调用。我提出了一个GET请求和POST请求。我也在使用笑脸来托管couchdb(他们有免费的托管)。以下是CORS配置:
凭证:假;标题:接受、授权、内容类型、来源;方法:获取、发布、放置、删除、选项、标题;来源:*
在windows上启动ZapWorks Studio时,一切都很好。但是,当使用android设备扫描zapcode时,post ajax调用失败。只有邮政。我正在使用基本认证。我强制要求只有管理员才能管理couchdb上的数据库。我可以访问主机从桌面和电话从网络浏览器,以手动完成一切。
我尽我所能解决这个问题:删除身份验证,更改CORS configuration...nothing工作。我认为这是CORS的一个问题,但是在窗口和手机上一切都很好,只是帖子失败了……我一直得到一个0的状态代码。
编辑-新信息,在阿比酯上的测试也在桌面和移动平台上工作。
编辑- 这是zpp以显示逻辑
在我的手机上尝试用REST Api客户编辑,它也起作用了。这只能是CORS问题或与zapworks有关的问题。奇怪的是,它在窗户上工作,但在电话上却不起作用。
编辑-我找出了问题所在,但没有找到解决办法。因此,我设置了一个代理来调试本教程之后从zapworks发出的请求。它似乎做了一个飞行前请求,但是得到了响应
“不允许使用HTTP/1.1 405方法”
即使有效载荷是
{“错误”:“method_not_allowed”,“原因”:“只允许删除、获取、头、发”}。
以下是请求:
OPTIONS /ranking HTTP/1.1
Host: somehost.com
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: null
User-Agent: Mozilla/5.0 (Linux; Android 8.0.0; SM-G950U1 Build/R16NW; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/67.0.3396.87 Mobile Safari/537.36
Access-Control-Request-Headers: authorization,content-type,x-requested-with
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US
X-Requested-With: com.zappar.Zappar以及答复:
HTTP/1.1 405 Method Not Allowed
Server: CouchDB/1.6.0 (Erlang OTP/R15B01)
Date: Mon, 18 Jun 2018 21:22:12 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 76
Cache-Control: must-revalidate
Allow: DELETE,GET,HEAD,POST
Access-Control-Expose-Headers: Cache-Control, Content-Type, Server
Access-Control-Allow-Origin: null
Connection: keep-alive
{"error":"method_not_allowed","reason":"Only DELETE,GET,HEAD,POST allowed"}这清楚地表明职位是被允许的..。
在窗口方面,似乎没有飞行前的请求,出于某种原因,我猜这就是为什么它的工作。现在的问题是如何将couchdb上的CORS配置为在android上工作。以下是可用的配置:
enable_cors: true
credentials: false
headers:Accept, Authorization, Content-Type, Origin
methods:GET,POST,PUT,DELETE,OPTIONS,HEAD
origins:*这是代码:
const Open_SansRegular_ttf0 = symbol.nodes.Open_SansRegular_ttf0;
parent.on("ready", () => {
const Plane0 = symbol.nodes.Plane0;
let ajaxParameters : Z.Ajax.Parameters = {
url: "https://something.smileupps.com/test/_all_docs?include_docs=true",
headers: {"Authorization": "Basic my64encoding"},
method: "GET",
timeout: 3000
};
// Perform the AJAX request
Z.ajax(ajaxParameters, (statusCode, data, request) => {checkRequest(statusCode, data);});
ajaxParameters = {
url: "https://something.smileupps.com/test",
headers: {"Content-Type":"application/json", "Authorization": "Basic my64encoding"},
method: "POST",
body: '{"name" : "asdasd", "something": 234}',
timeout: 3000
};
Z.ajax(ajaxParameters, (statusCode, data, request) => {checkRequest(statusCode, data);});
});
function checkRequest(statusCode, data) {
if (statusCode === 0) {
Open_SansRegular_ttf0.text("Unable to connect - check network connection.");
console.log("Unable to connect - check network connection.");
return;
}
if (statusCode < 200 || statusCode >= 300) {
Open_SansRegular_ttf0.text("HTTP request failed: " + statusCode);
console.log("HTTP request failed: " + statusCode);
return;
}
// Attempt to parse the data returned from the AJAX request as JSON
let parsedData;
try {
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
parsedData = JSON.parse(data);
} catch (e) {
Open_SansRegular_ttf0.text("Unable to parse JSON: " + e);
console.log("Unable to parse JSON: " + e);
return;
}
return parsedData;
}编辑这里是窗口上的请求
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US
Authorization:Basic mybase64encoding
Connection:keep-alive
Content-Length:37
Content-Type:application/json
Host:http://something.smileupps.com/test
Origin:file://
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ZapWorksStudio/4.0.4-stable Chrome/58.0.3029.110 Electron/1.7.9 Safari/537.36
X-DevTools-Request-Id:3680.9
X-Requested-With:XMLHttpRequest以及答复:
Access-Control-Allow-Origin:file://
Access-Control-Expose-Headers:Cache-Control, Content-Type, ETag, Server
Cache-Control:must-revalidate
Content-Length:95
Content-Type:text/plain; charset=utf-8
Date:Mon, 18 Jun 2018 21:36:22 GMT
ETag:"1-512f89feb3d0a88781119e772ec6fd7b"
Location:http://something.smileupps.com/test
Server:CouchDB/1.6.0 (Erlang OTP/R15B01)没有飞行前。
发布于 2018-06-19 12:47:22
您的问题在于请求:当使用file:而不是http或https协议打开包含xhr请求的网页时,通常会得到http。您将不会得到任何成功的CORS请求与这样的起源。
https://stackoverflow.com/questions/50877565
复制相似问题