本文介绍了如何在as.net中允许内容类型= application / json在asp.net中启用web api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to send an array of objects in my ajax call and for that i have to use json.stringify and after that i have to set content-type header as 'application/json' otherwise data will not be received by the server.:

here is the code for web api post method:








public bool Post(object [] list)

{

Newtonsoft.Json.Linq.JObject jObjOfSMaster =(Newtonsoft.Json.Linq.JObject)list [0];



TblSaleMaster obj = new TblSaleMaster();

obj = GetSaleMaster (jObjOfSMaster);



List< tblsalemasterdetail> listSMD = new list< tblsalemasterdetail>();

listSMD = GetSaleMasterDetail(list);





DBTSeedRepository repository = new DBTSeedRepository();

返回repository.InsertSale(obj,listSMD);

}




public bool Post(object[] list)
{
Newtonsoft.Json.Linq.JObject jObjOfSMaster = (Newtonsoft.Json.Linq.JObject)list[0];

TblSaleMaster obj = new TblSaleMaster();
obj = GetSaleMaster(jObjOfSMaster);

List<tblsalemasterdetail> listSMD = new List<tblsalemasterdetail>();
listSMD = GetSaleMasterDetail(list);


DBTSeedRepository repository = new DBTSeedRepository();
return repository.InsertSale(obj, listSMD);
}


这里我发布数据作为对象的结果



$('#btnSubmit ')。click(function(){

$ .ajax({

type:'POST',

url:'/ SeedPOSAPITest / SeedPOSAPI / api / POSTransaction',

数据:JSON.stringify(list),

dataType:'json',

contentType:'application / json; charset = utf-8',

proccessData:false,

成功:函数(数据){

alert(data);

}

});

});

here i am posting data as an arry of objects

$('#btnSubmit').click(function () {
$.ajax({
type: 'POST',
url: '/SeedPOSAPITest/SeedPOSAPI/api/POSTransaction',
data: JSON.stringify(list),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
proccessData: false,
success: function (data) {
alert(data);
}
});
});

推荐答案




这篇关于如何在as.net中允许内容类型= application / json在asp.net中启用web api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:43