Angular2 Http Post没有执行

时间:2016-06-24 10:12:23

标签: typescript angular

这是我从我的服务中调用它的方法

addData(data) {
    let _body = JSON.stringify(data);
    let headers = new Headers({'Content-Type': 'application/json;charset=utf-8'});

    let url = 'http://localhost:8080/obj/addData';
    let requestoptions:RequestOptions = new RequestOptions({
         method: RequestMethod.Post,
         url: url,
         headers: headers,
         body : _body
    });
    console.log("test");
    return this._http.request(new Request(requestoptions))
        .map((res:Response) => {
            console.log(res);
            return res.json();
        });
}

我添加了console.log以验证是否调用了该方法,因为虽然我调用了一个方法,但没有完成

2 个答案:

答案 0 :(得分:2)

在angular2 http请求中,直到您订阅http调用。因此,如果您将订阅添加到您在服务上调用方法的位置,如下所示:

// In some component or service using your service
this.service.addData(data).subscribe(resp => console.log(resp));

然后,在这种情况下,您应该看到发出请求并在控制台中发布响应数据。

答案 1 :(得分:0)

因为 string t1 = "SELECT C1,C2,C3,C4,C5 FROM TEST"; string t2 = "SELECT C1,C2,C3 FROM TEST"; List<string> test = CompareSQLString(t1, t2,"TEST"); 方法无法单独执行。鉴于您已经订阅tooltip.select('.label').html(donutChart()); function donutChart(){ var dataset = { hddrives: [20301672448, 9408258048, 2147483648, 21474836480, 35622912,32212254720], }; var width = 460, height = 300, radius = Math.min(width, height) / 2; var color = d3.scale.ordinal() .range(["#2DA7E2"]); var pie = d3.layout.pie() .sort(null); var arc = d3.svg.arc() .innerRadius(radius - 100) .outerRadius(radius - 70); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); var path = svg.selectAll("path") .data(pie(dataset.hddrives)) .enter().append("path") .attr("class", "arc") .attr("fill", function(d, i) { return color(i); }) .attr("d", arc); svg.append("text") .attr("dy", ".35em") .style("text-anchor", "middle") .attr("class", "inside") .text(function(d) { return 'Test'; }); } 方法,我建议使用request方法,而不是这样:

addData