首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角6:无法发出http请求

角6:无法发出http请求
EN

Stack Overflow用户
提问于 2018-06-11 08:34:31
回答 2查看 2K关注 0票数 1

在我的角度应用程序中,我试图向spring发出http post请求。但我无法成功..。我在浏览器控制台中没有收到任何错误响应。

我的角度代码是,

代码语言:javascript
复制
addToCart(productId: number, quantity: number) {
    const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
    console.log("--------order.service.ts----------addToCart()-------productId:"+productId+":------quantity:"+quantity);
     this.http.post('http://localhost:8080/order/addtocart', 
              '{ "dealerId": 9, "createdBy": "-1", "productId": productId, "quantity": quantity}', 
              {headers: headers})
              .pipe(catchError(this.errorHandlerService.handleError));
    }

Spring restful:

代码语言:javascript
复制
package com.wocs.rest.controller;

import java.io.IOException;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wocs.services.common.ServiceException;
import com.wocs.services.order.iface.OrderServiceIface;
import com.wocs.services.userrole.model.User;

@RestController()
@RequestMapping("/order")
public class OrderController {

    static Logger logger = Logger.getLogger(OrderController.class);

    @Autowired
    private OrderServiceIface orderService;

    public void setOrderService(OrderServiceIface orderService) {
        this.orderService = orderService;
    }

    @RequestMapping(value = "/addtocart", method = RequestMethod.POST, consumes = "text/plain")
    public void addToCart(@RequestBody String stringRequestBody) throws JsonParseException, JsonMappingException, IOException, ServiceException
    {
        logger.info("addtocart:"+stringRequestBody);
        Map<String, Object> jsonMap = new ObjectMapper().readValue(stringRequestBody,
            new TypeReference<Map<String,Object>>(){});

         orderService.addToCart((Integer)jsonMap.get("dealerId"), (String) jsonMap.get("createdBy"), (Integer)jsonMap.get("productId"), (Integer)jsonMap.get("quantity"));
    }

}

浏览器控制台:

任何帮助都将不胜感激..。提前谢谢..。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-11 09:00:01

你错过了订阅。如果您已在服务中声明了addToCart,并希望在组件中处理API响应,请将代码修改为:

服务

代码语言:javascript
复制
addToCart(productId: number, quantity: number) {
    let data = { 
        "dealerId": 9, 
        "createdBy": "-1", 
        "productId": productId, 
        "quantity": quantity
    }
    const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
    console.log("--------order.service.ts----------addToCart()-------productId:"+productId+":------quantity:"+quantity);
    return this.http.post('http://localhost:8080/order/addtocart', 
              JSON.stringify(data), 
              {headers: headers})
              .pipe(catchError(this.errorHandlerService.handleError));
}

组件订阅服务方法

代码语言:javascript
复制
this.service.addToCart(2, 4).subscribe(res => {
    console.log(res); // Response from API
})
票数 1
EN

Stack Overflow用户

发布于 2018-06-11 08:42:26

您没有订阅this.http.post(...)函数,它返回一个可观察到的函数。

可观察到的代码在订阅之前什么也不做,所以代码应该是:

代码语言:javascript
复制
addToCart(productId: number, quantity: number) {
    const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');

    this.http.post('http://localhost:8080/order/addtocart', 
          '{ "dealerId": 9, "createdBy": "-1", "productId": productId, "quantity": quantity}', 
          {headers: headers})
          .pipe(catchError(this.errorHandlerService.handleError))
          .subscribe(data => {
              // Handle the updated data here.
              console.log(data);
          });
}         

或者,如果您获得可以直接在视图中使用的任何数据,则可以使用异步管道来处理订阅。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50793488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档