[英]Make a POST request with JQuery to c# .net rest API
I am trying to make an ajax post request to a backend server that is built with .net framework.我正在尝试向使用 .net 框架构建的后端服务器发出 ajax post 请求。 The backend is a REST API built with c# .net.后端是使用 c# .net 构建的 REST API。 When i try to make a POST request with postman everything goes well, but when i try to do it with ajax, i dont get any data on the backend.当我尝试使用邮递员发出 POST 请求时一切顺利,但是当我尝试使用 ajax 执行此操作时,我没有在后端获得任何数据。 This is the C# code:这是 C# 代码:
public Documento()
{
this.DocContabilidade = new Dictionary<string, string>()
{
};
this.Linha1 = new Dictionary<string, string>()
{
};
this.Linha2 = new Dictionary<string, string>()
{
};
this.Linha3 = new Dictionary<string, string>()
{
};
}
public HttpResponseMessage Post([FromBody] Documento documento)
{
Console.WriteLine(documento);
// (when I make the request with postman the var has data, when i do it with ajax its empty)
.
.
var response = Request.CreateResponse(System.Net.HttpStatusCode.Created, "a");
return response;
}
when i try to send an object with postman, like I said it works well.当我尝试用邮递员发送对象时,就像我说的那样效果很好。 this is the postman data, its the same i send with ajax.这是邮递员数据,与我用ajax发送的数据相同。
{
"doccontabilidade": {
"ano": "2020",
"doc": "411",
"diario": "22",
"mes": "09",
"dia": "30",
"moeda": "EUR",
"modulo": "L",
"descricao":"FT54011481",
"num_doc_externo": "54011841"
},
"linha1": {
"tipo_linha": "F",
"conta": "211110001",
"natureza": "C",
"moeda": "EUR",
"tipo_entidade": "F",
"entidade": "FVD",
"valor": "529,02",
"valor_alt": "529,02",
"valor_origem": "529,02",
"descricao": "FT 9999",
"lote":"1"
},
"linha2": {
"tipo_linha": "F",
"conta": "31211",
"natureza": "D",
"moeda": "EUR",
"tipo_entidade": "F",
"entidade": "FVD",
"valor": "430,1",
"valor_alt": "430,1",
"valor_origem": "430,1",
"descricao": "FT 9999",
"lote":"2",
"iva":"12132311"
},
"linha3": {
"tipo_linha": "F",
"conta": "24321132311",
"natureza": "D",
"moeda": "EUR",
"tipo_entidade": "F",
"entidade": "FVD",
"valor": "98,92",
"valor_alt": "98,92",
"valor_origem": "98,92",
"descricao": "FT 9965",
"lote":"3"
}
}
this is the response from postman: postman response这是邮递员的回复:邮递员回复
the ajax request is as follows: ajax请求如下:
var dados = {
"doccontabilidade": {
"ano": "2020",
"doc": "411",
"diario": "22",
"mes": "09",
"dia": "30",
"moeda": "EUR",
"modulo": "L",
"descricao":"FT54011481",
"num_doc_externo": "54011841"
},
"linha1": {
"tipo_linha": "F",
"conta": "211110001",
"natureza": "C",
"moeda": "EUR",
"tipo_entidade": "F",
"entidade": "FVD",
"valor": "529,02",
"valor_alt": "529,02",
"valor_origem": "529,02",
"descricao": "FT 9999",
"lote":"1"
},
"linha2": {
"tipo_linha": "F",
"conta": "31211",
"natureza": "D",
"moeda": "EUR",
"tipo_entidade": "F",
"entidade": "FVD",
"valor": "430,1",
"valor_alt": "430,1",
"valor_origem": "430,1",
"descricao": "FT 9999",
"lote":"2",
"iva":"12132311"
},
"linha3": {
"tipo_linha": "F",
"conta": "24321132311",
"natureza": "D",
"moeda": "EUR",
"tipo_entidade": "F",
"entidade": "FVD",
"valor": "98,92",
"valor_alt": "98,92",
"valor_origem": "98,92",
"descricao": "FT 9965",
"lote":"3"
}
}
Ajax request: Ajax 请求:
$.ajax({
url: "https://192.168.33.122:44395/api/movimento",
type: 'POST',
data: JSON.stringify(dados),
dataType: 'application/json; charset=utf-8',
success: function (response) {
alert(response["resultado"])
},
failure: function (response) {[enter image description here][1]
console.log('Erro');
}
});
and this is the response from ajax: ajax response这是来自 ajax 的响应: ajax response
I tried to send the data without the JSON.stringify but C# still cant detect anything.我尝试在没有 JSON.stringify 的情况下发送数据,但 C# 仍然无法检测到任何内容。
The way I do it and is working is:我这样做和工作的方式是:
var dados= { dados: [... your elements ...]};
$.ajax({
type: 'post',
url: 'Your-URI',
data: JSON.stringify(dados),
contentType: "application/json; charset=utf-8",
cache: false,
traditional: true,
success: function (response) {
alert(response["resultado"])
},
failure: function (response) {
console.log('Erro');
}
});
or或者
$.ajax({
url: "@Url.Action("Your-METHOD", "Your-Controller")",
type: "POST",
contentType: "application/json",
data: JSON.stringify({ dados: Documento}),
traditional: true,
success: function(response) {
response ? alert("Processing") : alert("Error");
}
});
and in backend并在后端
[HttpPost]
public async Task<JsonResult> JsonResponseMessage(Documento doc)
.....
It is better if the document data structure is the same as the response data json.文档数据结构最好和响应数据json一样。 You can convert the json response with JsonToSchemaC# Hope it helps.您可以使用JsonToSchemaC#转换 json 响应希望它有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.