SpringMVC axios receiving asynchronous data POST request submitted

1.axios the post request is submitted, the form data as submitted, we want to use some of the data submitted post axios the header can be set to become "Content-Type": "application/x-www-form-urlencoded";
 axios
   .post(this.HOME + "/AwesomePos/order/add", data, {
     headers: { "Content-Type": "application/x-www-form-urlencoded" }
   })
   .then(response => {
     if (response.data.code == 1) {
       this.totalMoney = 0;
       this.tableData = [];
       this.totalCount = 0;

       this.$message({
         message: "结账成功,感谢你又为店里出了一份力!",
         type: "success"
       });
     } else {
       this.$message.error("网络异常,结账失败请稍后重试!");
     }
   }).defaults.headers.post["Content-Type"] = "application/json";

You should upload the configuration file in the configuration file related to dependence SpringMVC

<!--MultipartResolve,用于文件上传,使用spring的CommmonsMultipartResolver-->
 <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <property name="maxUploadSize" value="5000000"/>
     <property name="defaultEncoding" value="UTF-8"/>
 </bean>

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
2. The required data filed in the above code segment data, the dataneed is a formDatatype of data, the data before submitting the data need to be submitted to encapsulation.
  var order = {
      orderHash: this.uuid,
      totalCount: this.totalCount,
      amount: this.totalMoney,
      orderStatus: 1,
      payStatus: 1
    };

    var orderDetailArr = this.tableData;

    var tableData_ = {
      order: order,
      orderDetailArr: orderDetailArr
    };
		
	//封装需要提交的数据为FormData类型
    let data = new FormData();
    //将tableData转换成JSON的字符串形式 方便后端服务器接收
    data.append("tableData", JSON.stringify(tableData_));

    axios
      .post(this.HOME + "/AwesomePos/order/add", data, {
        headers: { "Content-Type": "application/x-www-form-urlencoded" }
      })
      .then(response => {
        if (response.data.code == 1) {
          this.totalMoney = 0;
          this.tableData = [];
          this.totalCount = 0;

          this.$message({
            message: "结账成功,感谢你又为店里出了一份力!",
            type: "success"
          });
        } else {
          this.$message.error("网络异常,结账失败请稍后重试!");
        }
      }).defaults.headers.post["Content-Type"] = "application/json";
3. The back-end processor to receive a request parameter, as JSON data has been converted into a string using the parameter so that the rear end of the processor in the previous step of the method Stringtype received on it, within the method according to the parameter type parameter passed converted to the specified format, if it is then converted to an array JSONArray, then how is a JavaBean convert JSONObject, and then finally JSONObject/JSONArrayconverted into JavaBean objects specified.
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object addOrderInfo(@RequestParam(value = "tableData") String JsonObj){

        logger.info(JsonObj);

        JSONObject tableData=JSONObject.parseObject(JsonObj);

        JSONObject orderObj=tableData.getJSONObject("order");

        JSONArray orderDetailObj=tableData.getJSONArray("orderDetailArr");

        Order order= (Order) JSON.parseObject(orderObj.toString(),Order.class);
        OrderDetail[]orderDetailArr=(OrderDetail[]) JSON.parseObject(orderDetailObj.toString(),OrderDetail[].class);

        order.setCreatedBy(1);
        order.setCreationDate(new Date());
        order.setCustomerId(1);

        logger.info("=========================exec addOrderInfo()=====================================");
        logger.info("orderHash====>"+order.getOrderHash());
        logger.info("orderAmount=======>"+order.getAmount());
        logger.info("customerId=========>"+order.getCustomerId());
        logger.info("orderStatus====>"+order.getOrderStatus());
        logger.info("payStatus=====>"+order.getPayStatus());
        logger.info("orderDetailArr=====>"+orderDetailArr.toString());

        //创建订单信息
        boolean flag= orderServiceImpl.addOrderInfo(order, Arrays.asList(orderDetailArr));

        HashMap<String,String> resultMap=new HashMap<>();

        if(flag){
            resultMap.put("msg", "SUCCESS");
            resultMap.put("code", "1");
        }else{
            resultMap.put("msg", "FAILED");
            resultMap.put("code", "0");
        }

        return resultMap;
    }
Published 56 original articles · won praise 17 · views 6191

Guess you like

Origin blog.csdn.net/qq_43199016/article/details/103097487