freeds
freeds
采纳率50%
2018-11-05 04:50 阅读 1.1k

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

10

$.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条回答 默认 最新

  • 已采纳
    freeds freeds 2018-11-05 08:40

    我自己找到方法了,调用应改成
    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);
            });
        }
    
    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2018-11-05 05:06

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

    $.post代替$.ajax看看呢

    点赞 1 评论 复制链接分享

相关推荐