我正在尝试做一些应该简单的事情:向网址发出GET请求.然而,当我寻找的是如何做到这一点我经常例子风与像近乎胡言乱语此.
有谁知道如何使用OCaml发出简单的HTTP请求?我是一个带有一些Haskell exp的OCaml新手.
注意:
使用尽可能低的OCaml的解决方案将是理想的.我已经看过Cohttp使用过这个库了,但我对本机(?)HTTPOCaml lib或者其他类似的东西更感兴趣.
响应于@antron,将非常感谢使用最低可能级别的本地OCaml的解决方案.我被引导相信这将涉及Unix图书馆.但是,如果有另一个不涉及第三方库的解决方案,那将同样受欢迎.
我正在使用Angular 1.5.8.这是我的代码:
describe('My Controller', function() {
var MyController;
var $controller;
var $rootScope;
var $state;
beforeEach(angular.mock.module('ui.router'));
beforeEach(module('app.my.ctrl'));
beforeEach(inject(function(_$controller_, _$rootScope_, _$state_) {
$controller = _$controller_;
$rootScope = _$rootScope_;
$state = _$state_;
MyController = $controller('MyController', { scope: $rootScope.$new() });
}));
describe('#init', function() {
it('should do something', function() {
console.log('logStatement', MyController);
MyController.init();
expect(true).toBe(true);
})
})
});
Run Code Online (Sandbox Code Playgroud)
测试运行器能够找到所有文件,所以这不是忘记加载某些东西的情况.当我运行此测试时,不仅logStatement没有出现,我收到此错误:
Argument 'MyController' is not a function, got undefined
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
(function() {
'use strict';
angular
.module('app.my.ctrl')
.controller('MyController', MyController);
MyController.$inject = [
'$scope'
];
/* ngInject */ …Run Code Online (Sandbox Code Playgroud) 我试图从头开始这样做,而不使用标准库之外的库.继承我的代码:
permutations :: [a] -> [[a]]
permutations (x:xs) = [x] : permutations' xs
where permutations' (x:xs) = (:) <$> [x] <*> split xs
split l = [[x] | x <- l]
Run Code Online (Sandbox Code Playgroud)
问题是这只产生一个非确定性计算的分支.理想情况下我想要
(:) <$> [x] <*> ((:) <$> [x] <*> ((:) <$> [x] <*> ((:) <$> [x] <*> xs)))
Run Code Online (Sandbox Code Playgroud)
但我找不到干净利落的方法.我想要的结果是这样的:
permutations "abc" -> ["abc", "acb", "bac", "bca", "cab", "cba"]
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
这是我解释递归的书中的代码.问题是我不明白该计划采取的步骤:
var hanoi = function(disc,src,aux,dst) {
if (disc > 0) {
hanoi(disc - 1,src,dst,aux);
document.write("Move disc " + disc + " from " + src + " to " + dst + "<br />");
hanoi(disc - 1,aux,src,dst);
}
};
hanoi(3,"src","aux","dst");
Run Code Online (Sandbox Code Playgroud)
这是输出的读取方式:
Move disc 1 from src to dst
Move disc 2 from src to aux
Move disc 1 from dst to aux
Move disc 3 from src to dst
Move disc 1 from aux to src
Move disc 2 from …Run Code Online (Sandbox Code Playgroud) 我正在使用Raphael.js.每次我加载页面时都会收到一条错误,内容如下:
con is undefined
x = con.x
Run Code Online (Sandbox Code Playgroud)
我抬头看了con拉斐尔的文档,这就是我发现的:
var con = R._getContainer.apply(0, arguments),
container = con && con.container,
x = con.x,
y = con.y,
width = con.width,
height = con.height;
//...
Run Code Online (Sandbox Code Playgroud)
con在这里明确定义.这是我要加载的代码:
var paper = new Raphael(ele('canvas_container'), 500, 500);
window.onload = function() {
var circle = paper.circle(100,100,100);
for (i = 0; i < 5; i++) {
var multiplier = i * 5;
paper.circle(250 + (2 * multiplier), 100 + multiplier, 50 - multiplier);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有其他人得到这个错误?这是拉斐尔版本中的一个错误吗?我还是有其他问题吗?
我正在尝试在可以覆盖的记录对象中设置默认值.
data Car c y = Car {
company :: c,
year :: y
}
Run Code Online (Sandbox Code Playgroud)
我想做的是设置默认值:
data Car c y = Car {
company :: c -- OR "Ford"
year :: y
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试通过将c的Maybe类型设置为类型来执行此操作:
data Car = Car {
company:: Maybe String
year :: Maybe Int
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个可预测的错误:
Fields of `Car' not initialised: year
Run Code Online (Sandbox Code Playgroud)
具有讽刺意味的是,这正是我想要解决的问题.我想生成一个新记录,其中包含我未设置的值已经初始化.我发现的一种方法是部分应用Car类型:
data Car c y = {
company :: c,
year :: y
}
let ford = Car "Ford"
-- This produces …Run Code Online (Sandbox Code Playgroud) 我发现了一个可能相关的问题,需要在这里提出一个新问题
这是一个奇怪的问题.我在2年的时间里一直使用角度,从来没有遇到过这个问题.
我正在使用angular v1.5.0.我正在发布这样的帖子请求:
$http({
method: "POST",
url: "/myurl",
data: {
file: myFile // This is just an object
}
});
Run Code Online (Sandbox Code Playgroud)
普通的POST请求对吗?得到这个.我查看控制台,"网络"选项卡将请求记录为GET.离奇.所以我已经开始像这样工作:
$http.post("/myurl", {file: myFile});
Run Code Online (Sandbox Code Playgroud)
一样.单步执行$http服务代码后,我确信正在正确设置标头.有没有其他人遇到这个问题?
考虑到德国人的建议,我尝试使用该$resource服务:
promise = $resource("/upload").save()
Run Code Online (Sandbox Code Playgroud)
(由于其他原因,这会返回错误,它仍然可以正确执行POST).我遇到了同样的问题:请求在控制台中记录为GET.
以下是请求到达我的服务器时的标头:
GET /myurl/ HTTP/1.1
Host: localhost:8001
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cache-Control: no-cache
Connection: keep-alive
Pragma: no-cache
Referer: http://localhost:8001/myurl/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
Run Code Online (Sandbox Code Playgroud)
Here's my code:
# models.py
class MyModel(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=10)
...
# views.py
def get_all_models(request):
return JsonResponse({"models": list(MyModel.objects.all())})
# urls.py
path('/mypath', views.get_all_models, name='get_all_models'),
Run Code Online (Sandbox Code Playgroud)
This code works just fine if I visit /mypath. However, when I run an automated test using Django's test client, I get this error:
*** TypeError: Object of type MyModel is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
this is my test: from django.test import TestCase, Client from blog.tests.factories.user import UserFactory from blog.tests.factories.post import PostFactory
class …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的网站上启用谷歌登录.该按钮有效,与我的帐户同步,但我无法从谷歌访问userId.这就是我的想法.
<script type="text/javascript">
(function() {
var po = document.createElement('script');
po.type = 'text/javascript'; po.async = true;
po.src = 'https://plus.google.com/js/client:plusone.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
})();
</script>
Run Code Online (Sandbox Code Playgroud)
这是我试图获取用户ID的地方.在控制台中,我收到了Uncaught ReferenceError: gapi is not defined.我认为我gapi在上面的源代码中调用的错误消息.任何帮助或建议将不胜感激.
$('document').ready(function(){
var request = gapi.client.plus.people.get({
'userId' : 'me'
});
request.execute(function(resp) {
console.log('ID: ' + resp.id);
console.log('Display Name: ' + resp.displayName);
console.log('Image URL: ' + resp.image.url);
console.log('Profile URL: ' + resp.url);
});
});
Run Code Online (Sandbox Code Playgroud) 为了记录,我是一个Haskell noob.我试图理解为什么范围可以声明为
[1..10] -- yields [1,2,3,4,5,6,7,8,9,10]
Run Code Online (Sandbox Code Playgroud)
并不是
[10..1] -- yields []
Run Code Online (Sandbox Code Playgroud)
看起来很简单,就像这样实现它:
(.:.) :: Enum a => a -> a -> [a]
(.:.) a b =
| a == b = []
| a > b = a : (a - 1) .:. b
| a < b = a : (a + 1) .:. b
Run Code Online (Sandbox Code Playgroud)
我在这里不理解什么?
haskell ×3
javascript ×3
angularjs ×2
http ×2
angular-http ×1
django ×1
go ×1
google-api ×1
html ×1
jasmine ×1
ocaml ×1
python ×1
raphael ×1
record ×1
recursion ×1