我有一个Play 2.0.1应用程序,我想使用托管在其他域上的Javascript调用.我的Javascript调用失败了:
Origin http://mydomain.com is not allowed by Access-Control-Allow-Origin.
Run Code Online (Sandbox Code Playgroud)
我已经找到了一些如何在Play 1中设置正确的HTTP标头的例子,但是没有为Play 2.0.1找到任何东西.在阅读文档(http://www.playframework.org/documentation/2.0.2/JavaResponse)之后,我尝试了以下方法以使工作正常:
public static Result myJsonWebService() {
...
response().setHeader("Access-Control-Allow-Origin", "*");
return ok(toJson(jsonObject));
}
Run Code Online (Sandbox Code Playgroud)
但我的JS Web服务调用仍然失败.
为了让这个工作,我需要做什么?
我在 Python 3.8 中遇到一个奇怪的错误:
当我运行这个计算时,我得到一个复数:
>>> (1.0 / (2.0 - 0.5222232592740141 * 92.16159106468214)) ** (1.0 / (10.0 + 1))
(0.6772850578932906+0.1988688362687656j)
Run Code Online (Sandbox Code Playgroud)
但是,如果我在 Python 中手动计算,我会得到一个浮点答案:
>>> (1.0 / (2.0 - 0.5222232592740141 * 92.16159106468214))
-0.021678371395529073
>>> (1.0 / (10.0 + 1))
0.09090909090909091
>>> -0.021678371395529073 ** 0.09090909090909091
-0.7058780799007794
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
我正在尝试创建一个简单的应用程序,允许我创建,读取,更新和删除各种用户.我有一个基本的基于UI的视图,控制器和模型,但希望比这更高级,并提供RESTful json接口.
然而,尽管阅读了我在Play 2文档,Play 2 Google组和stackoverflow网站上可以找到的所有内容,但我仍然无法使用它.
我已根据之前的反馈更新了我的控制器,现在我相信它是基于文档的.
这是我更新的控制器:
package controllers;
import models.Member;
import play.*;
import play.mvc.*;
import play.libs.Json;
import play.data.Form;
public class Api extends Controller {
/* Return member info - version to serve Json response */
public static Result member(Long id){
ObjectNode result = Json.newObject();
Member member = Member.byid(id);
result.put("id", member.id);
result.put("email", member.email);
result.put("name", member.name);
return ok(result);
}
// Create a new body parser of class Json based on the values sent in the POST
@BodyParser.Of(Json.class)
public static Result …Run Code Online (Sandbox Code Playgroud) 我想使用带有 Three.js 的预先创建的画布。从我读过的教程和其他帖子来看,这应该有效:
const canvas = document.createElement('canvas');
const ctx = canvas.getContext("2d"); // <--- This causes the error below!
const renderer = new THREE.WebGLRenderer( { canvas: canvas } );
Run Code Online (Sandbox Code Playgroud)
但是,在我的浏览器控制台(Safari v14 和 Chrome v86)中,我收到以下错误:
THREE.WebGLRenderer: Error creating WebGL context.
Run Code Online (Sandbox Code Playgroud)
我也尝试过添加
<canvas id='myCanvas'></canvas>
Run Code Online (Sandbox Code Playgroud)
并使用:
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d"); // <--- This causes the same error!
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
});
Run Code Online (Sandbox Code Playgroud)
并得到同样的问题。
我也尝试添加:
window.onload = function() {
...
};
Run Code Online (Sandbox Code Playgroud)
确保 DOM 已加载等
如果我删除这些getContext("2d")行,那么它可以工作吗?
我使用的是 …