报错 Request method 'GET' not supported

报错 Request method 'GET' not supported

$(function(){
	var listUrl = '/o2o/productadmin/getproductcategorylist';
	var addUrl = '/o2o/productadmin/addproductcategorys';
	getList();
	
	function getList(){
		$.getJSON(
			listUrl,
			function(data){
				console.log(data.data);
				if(data.success){
					var datalist = data.data;
					
					$('.product-category-wrap').html('');
					var temphtml='';
					datalist.map(function (item,index){
						temphtml += '<div class="row row-product-category now">'
						+'<div class="col-33 product-category-name">'
						+ item.productCategoryName
						+'</div>'
						+'<div class="col-33">'
						+item.priority
						+'</div><div class="col-33" ><a href="#" class="button delete" data-id="'
						+item.productCategoryId
						+'">删除</a></div>'
						+'</div>';
			
						
					});
					$('.product-category-wrap').append(temphtml);
				}
			}
		);
	}


    // 新增添加的输入项
    $('#new').click(function () {
        var tempHTML = '<div class="row row-product-category temp"><div class="col-33">' +
                       '<input class="category-input category" type="text" placeholder="分类名"></div>' +
                       '<div class="col-33"><input class="category-input priority" type="number" placeholder="优先级"></div>' +
                       '<div class="col-33"><a href="#" class="button delete">删除</a></div></div>';
        // 追加
        $('.product-category-wrap').append(tempHTML);
    });

$('#submit').click(function(){
	var tempArr = $('.temp');
	var productCategoryList = [];
	tempArr.map(function(item,index){
		var tempObj = {};
		tempObj.productCategoryName = $(index).find('.category').val();
		tempObj.priority = $(index).find('.priority').val();
		if(tempObj.productCategoryName != null && tempObj.priority != null){
			productCategoryList.push(tempObj);
		}
	});
	console.log(JSON.stringify(productCategoryList));
	$.ajax({
		url:addUrl,
		type:'POST',
		date:JSON.stringify(productCategoryList),
		contentType:'application/json;character:utf-8',
		success:function(data){
			if(data.success){
				$.toast('提交成功');
				getList();
			}else{
				$.toast('提交失败');
			}
		}
	});
});

});

package com.myk.o2o.web.productadmin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;

import com.myk.o2o.dto.ProductCategoryExecution;
import com.myk.o2o.dto.Result;
import com.myk.o2o.entity.ProductCategory;
import com.myk.o2o.entity.Shop;
import com.myk.o2o.enums.ProductCategoryStateEnum;
import com.myk.o2o.exceptions.ProductCategoryOperationException;
import com.myk.o2o.service.ProductCategoryService;
import com.myk.o2o.util.HttpServletRequestUtil;

@Controller
@RequestMapping("/productadmin")
public class ProductManagentController {
	
	@Autowired
	private ProductCategoryService productCategoryService;
	
	@RequestMapping(value="/getproductcategorylist",method=RequestMethod.GET)
	@ResponseBody
	private Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request){
		Shop shop = (Shop) request.getSession().getAttribute("currentShop");
		List<ProductCategory> list = new ArrayList<ProductCategory>();
		if (shop != null && shop.getShopId() > 0) {
			list = productCategoryService.getProductCategoryList(shop.getShopId());
			return new Result<List<ProductCategory>>(true,list);
		}else {
			ProductCategoryStateEnum ps = ProductCategoryStateEnum.INNER_ERROR;
			return new Result<List<ProductCategory>>(false,ps.getState(),ps.getStateInfo());
		}
	}
	
	@RequestMapping(value="/addproductcategorys",method=RequestMethod.POST,produces = "application/json;character:utf-8")
	@ResponseBody
	private Map<String ,Object> addProductCategorys(@RequestBody List<ProductCategory> productCategoryList,HttpServletRequest request){
		Map<String ,Object> modelMap = new HashMap<String, Object>();
		Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
		System.out.println(productCategoryList.size());
		for (ProductCategory productCategory : productCategoryList) {
			productCategory.setShopId(currentShop.getShopId());
		}
		if (productCategoryList != null && productCategoryList.size() >0) {
			try {
				ProductCategoryExecution pe = productCategoryService.batchAddProductCategory(productCategoryList);
				if (pe.getState()  == ProductCategoryStateEnum.SUCCESS.getState()) {
					modelMap.put("success", true);
				}else {
					modelMap.put("success", false);
					modelMap.put("errMsg",pe.getStateInfo());
				}
			}catch (ProductCategoryOperationException e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
				return modelMap;
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "请至少输入一个商品类别");
		}
		return modelMap;
	}
}

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿慧 2021-04-14 10:50:39

同学你好,老师这边测试同学代码没有问题,可以使用GET方式请求/o2o/productadmin/getproductcategorylist,如下:

http://img1.sycdn.imooc.com//climg/60765836098ff7c711360894.jpg

建议同学重启Tomcat,再试试。同学可以运行课程源码试试。

祝学习愉快~

  • 提问者 慕粉1705037409 #1

    我重启过来   包括eclipse和浏览器  也清空缓存了

    2021-04-14 11:38:21
  • 好帮手慕阿慧 回复 提问者 慕粉1705037409 #2

    ​同学你好,建议同学下载课程源码,运行课程源码试试。 

    祝学习愉快~

    2021-04-14 18:02:51
  • 提问者 慕粉1705037409 #3

    Failed to read HTTP message  修改请求方式为GET后 报错

    2021-04-14 21:48:36
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师