Jquery调用asp.net core web api开发的restful,post类型响应的却是get方法

$.ajax({
type: "POST",
url: "http://localhost:4413/api/WLCode/",
contentType: "application/json; charset=utf-8",
dataType: 'jsonp',
crossDomain: true,
data: { "prod_group": "group1", "prod_type": "type1", "prod_style": "style1", "prod_code": "wlcode1", "prod_desc": "物料", "prod_unit": "个", "is_used": "1" },
success: function (data) { alert(JSON.stringify(data)); },
error: function (data) {
debugger
alert('添加失败!');

            }
        });
                    上面是调用代码,响应的却是下面get方法

    // GET: api/WLCode
    [HttpGet]
    public IEnumerable<string> Get()
    {

        return new string[] { "value1", "value2" };
    }
            下面是post方法代码
              [HttpPost]
    public string Post([FromBody]WLCode wlcode)
    {
        string msg = string.Empty;
        DbHelperSql helperSql = new DbHelperSql(ConnectionString.DefaultConnection);
        if (string.IsNullOrEmpty(wlcode.prod_group))
        {
            msg += "物料组,";
        }
        if (string.IsNullOrEmpty(wlcode.prod_type))
        {
            msg += "物料小类,";
        }
        if (string.IsNullOrEmpty(wlcode.prod_type))
        {
            msg += "物料类型,";
        }
        if (string.IsNullOrEmpty(wlcode.prod_code))
        {
            msg += "SAP物料编码,";
        }
        if (string.IsNullOrEmpty(wlcode.prod_desc))
        {
            msg += "物料名称,";
        }
        if (string.IsNullOrEmpty(wlcode.prod_unit))
        {
            msg += "主计量单位,";
        }
        if (!string.IsNullOrEmpty(msg))
        {
            msg += msg.TrimEnd(',');
            msg += "不能为空";
        }
        else
        {
            try
            {
                string strsql = "INSERT INTO InterFace_WLCode(prod_group,prod_type,prod_style,prod_code,zl_prod_code,zt_prod_code,prod_desc,long_desc,prod_unit, is_used) "
              + "VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')";
                int i = helperSql.ExecuteSql(string.Format(strsql, wlcode.prod_group, wlcode.prod_type, wlcode.prod_style, wlcode.prod_code, wlcode.zl_prod_code, wlcode.zt_prod_code, wlcode.prod_desc, wlcode.long_desc, wlcode.prod_unit, wlcode.is_used));
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
        }

        return msg;

    }
            用postman工具测试是没问题的,但是用自己写的jQuery调用就会有问题,用f12追踪不知道怎么类型也是get。我调用方法明明写的是type=post.请哪位大神指教一下

2个回答

我自己找到方法了,调用应改成
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:4413/api/WLCode",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"cache-control": "no-cache"
},
"processData": false,
"data": "{\"prod_group\":\"2\",\"prod_type\":\"32\",\"prod_style\":\"test\",\"prod_code\":\"test123\",\"zl_prod_code\":\"code1\",\"zt_prod_code\":\"wee\",\"prod_desc\":\"wewrewer\",\"long_desc\":\"wewr\",\"prod_unit\":\"g\", \"is_used\":\"1\"}"
}

        $.ajax(settings).done(function (response) {
            console.log(response);
        });
    }

你f12看到是get么?那么就不是服务器的事情

$.post代替$.ajax看看呢

freeds
freeds 用postman测试是没问题的
大约一年之前 回复
freeds
freeds $.post没反应
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问