NetCore WebAPI

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/farmwang/article/details/85240589
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Web;
using Microsoft.AspNetCore.Http;


// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace QMNetCore.Service
{
    [Route("Service/{controller}/{action}")]
    public class MenuService : Controller
    {
         
        [HttpPost]
        public void PostData([FromBody]StdReq req)
        {
            StdRsp rsp = new StdRsp();
            
            try
            {
               Console.WriteLine( HttpContext.Request.Path);

               
                Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(req));
                rsp.code = "000";
                rsp.msg = "Good Hello world===" + req.keyid;
            }
            catch (Exception ex)
            {
                rsp.code = "200";
                rsp.msg = ex.ToString().Substring(0, 100);
            }

           HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
           HttpContext.Response.ContentType = "application/json";
            //  return   Newtonsoft.Json.JsonConvert.SerializeObject(rsp);
            HttpContext.Response.WriteAsync(Newtonsoft.Json.JsonConvert.SerializeObject(rsp));

        }
         
    }


    public class StdRsp
    {
        public string code { set; get; }
        public string msg { set; get; }
        public DataTable data { set; get; }
    }


    public class StdReq
    {
        public string keyid { set; get; }
        public string info { set; get; }
        public string P1 { set; get; }
        public DataTable data { set; get; }
    }
}
    $.ajax({
                    contentType: 'application/json',
                    url: "http://localhost:5000/Service/MenuService/PostData",
                    type: "post",
                    //crossDomain: true,
                    timeout: 60000,
                    beforeSend: function (XMLHttpRequest) {
                        QMSetBlockUI(9000);
                    },
                    data: JSON.stringify(GetJsonData1()),
                    success: function (rsp) {

                        console.log(rsp);
                        
                        if (rsp.code == '000') {
                             QMRsp.SetRspMsg('OK;'+rsp.msg);
                        }
                        else {
                             QMRsp.SetRspMsg('NG;更新失败!');
                        }
                    },
                    complete: function (XMLHttpRequest, textStatus) {
                        $.unblockUI();
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        QMRsp.SetRspMsg('NG;' + jqXHR.status + '  ' + jqXHR.readyState + ' ' + jqXHR.statusText + '  ' + jqXHR.status + '  ' + textStatus + '  ' + errorThrown);
                    }
                });

猜你喜欢

转载自blog.csdn.net/farmwang/article/details/85240589