我有一个react/redux应用程序,我正在尝试对服务器执行简单的GET请求:
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
}).then((response) => {
console.log(response.body); // null
return dispatch({
type: "GET_CALL",
response: response
});
})
.catch(error => { console.log('request failed', error); });
Run Code Online (Sandbox Code Playgroud)
问题是.then()函数中的响应体是空的,我不知道为什么.我在网上检查了一些例子,看起来我的代码应该可行,所以我显然在这里遗漏了一些东西.问题是,如果我检查Chrome的开发工具中的网络选项卡,则会发出请求并收到我正在寻找的数据.
任何人都可以对这一个发光吗?
编辑:
我尝试转换响应.
使用.text():
fetch('http://example.com/api/node', {
mode: "no-cors",
method: "GET",
headers: {
"Accept": "application/json"
}
})
.then(response => response.text())
.then((response) => {
console.log(response); // returns empty string
return dispatch({
type: "GET_CALL",
response: response
});
})
.catch(error => { console.log('request failed', error); }); …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Angular指令创建d3图表.我设法创建它,但问题是我想在图表元素上有一些ng-click事件,我不确定应该怎么做.
这是我的指示:
.directive('circleChart', function($parse, $window, $compile) {
return {
restrict: 'A',
scope: {
datajson: '='
},
link: function(scope, elem, attrs) {
var circleChart = new CircleChart(scope.datajson);
circleChart.initialise(scope);
var svg = circleChart.generateGraph();
svg = angular.element(svg);
console.log(svg);
//scope.$apply(function() {
var content = $compile(svg)(scope);
elem.append(content);
//});
}
}
});
Run Code Online (Sandbox Code Playgroud)
CircleChart对象创建了我的d3图表,并且我将地图附加了一个ng-click属性的地方(似乎不是一个正确的Angular方式):
var CircleChart = Class.create({
initialise: function(scope) {
this.datajson = scope.datajson;
},
generateGraph: function() {
.............
var chartContent = d3.select("div#chart-content");
var svg = chartContent.append("svg")
.attr("id", "circle")
.attr("width", diameter)
.attr("height", diameter)
.style("border-radius", "50px")
.append("g")
.attr("transform", …Run Code Online (Sandbox Code Playgroud)