代码之家  ›  专栏  ›  技术社区  ›  rahul shalgar

角度6:无法发出HTTP请求

  •  0
  • rahul shalgar  · 技术社区  · 6 年前
    addtocart(productid:number,quantity:number){
    this.http.post('http://localhost:8080/order/addtocart',
    标题:标题)
    
    
    
    导入java.io.ioexception;
    
    导入org.apache.log4j.logger;
    导入org.springframework.web.bind.annotation.requestbody;
    导入org.springframework.web.bind.annotation.requestmethod;
    
    导入com.fasterxml.jackson.core.type.typereference;
    导入com.fasterxml.jackson.databind.objectmapper;
    导入com.wocs.services.order.iface.orderServiceiface;
    
    @RestController()。
    公共类OrderController{
    
    
    私人订单服务如果空间订单服务;
    
    }
    
    {
    新建typereference<map<string,object>>());
    
    }
    
    
    

    浏览器控制台:

    浏览器控制台中的ONSE。

    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应用程序接口:

    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"));
        }
    
    }
    

    enter image description here

    2 回复  |  直到 6 年前
        1
  •  1
  •   Anshuman Jaiswal    6 年前

    您错过了订阅。如果你已经声明 addToCart

    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));
    }
    

    成分

    this.service.addToCart(2, 4).subscribe(res => {
        console.log(res); // Response from API
    })
    
        2
  •  1
  •   skovmand    6 年前

    this.http.post(...) 函数,返回可观察的。

    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);
              });
    }         
    

    或者,您可以使用 async pipe