After further analyzing, I believe the only problem was the order of the parameters in your url, for example, you were doing so:
$.ajax({
url: '@Url.Action("ClienteConfiguracaoPlano","ObtemContaPagamento")',
type: "POST",
data: { id: 21},
cache: false,
async: true,
success: function (data) {
alert(data);
}
});
while the correct url would be:
$.ajax({
url: '@Url.Action("ObtemContaPagamento", "ClienteConfiguracaoPlano")',
type: "POST",
contentType: 'application/json; charset=UTF-8',
data: JSON.stringify({ id: 21}),
dataType: 'json',
cache: false,
async: true,
success: function (data) {
alert(data);
}
});
because first you must pass the name of Action, and then the name of the Controller.
Note also that I used:
contentType: 'application/json; charset=UTF-8',
data: JSON.stringify({ id: 21}),
dataType: 'json',
So that the data sent and received are by JSON.
Well, as I said in the messages, I suggest you change your Action method to [HttpGet]
instead of [HttpPost]
, since your request only gets data and does not change anything.
This way it is safer, because the method [HttpGet]
only accepts the data request and not their modification on the server.
There are cases that will need to use the [HttpPost]
, when for example you have to pass as Action parameter, some more complex data, such as a class that has some property with some more complex type, i.e., Ienumerable or some other class within it. In this case you would have to use the [HttpPost]
for the [HttpGet]
only accepts simple parameters, for example: { parametro: valor }
.
In this case then, your request would look like this:
$.get('@Url.Action("ObtemContaPagamento", "ClienteConfiguracaoPlano")', { id: 21 }, function(data) {
alert(data);
});
Another suggestion I give is to use a more user-friendly form of url, for example:
$.get('/ClienteConfiguracaoPlano/ObtemContaPagamento', { id: 21 }, function(data) {
alert(data);
});
Or better yet, so I don’t have trouble with more routes, I usually do the following.
In my HTML I put an input to type Hidden with the url inside and get this url by javascript, this way I think it is more typed and safe, this way:
In HTML:
<input type="hidden" id="url-obtem-conta-pagamento" value="@Url.Action("ObtemContaPagamento", "ClienteConfiguracaoPlano")" />
In javascript:
var urlObtemContaPagamento = $('#url-obtem-conta-pagamento').val();
$.get(urlObtemContaPagamento, { id: 21 }, function(data) {
alert(data);
});
This way you make sure you are taking the right route and get more typed.
To better understand routes in ASP.NET MVC, I found this article which explains exactly how it works in a very clear way.
I hope I’ve helped.
Hug!
Have you ever thought of making a request ajax inside the changeSaldo function to fetch the data through the Obtemaccount?
– Leandro Simões
I don’t know how to do... that’s problem I tried
$.ajax({
 url: '@Url.Action("ClienteConfiguracaoPlano","ObtemContaPagamento")',
 type: "POST",
 data: { id: 21},
 cache: false,
 async: true,
 success: function (data) {
 alert(data);
 }
– Marco Souza
but it didn’t work out
– Marco Souza
Change your method to
[HttpGet]
and try to do so:$.get('@Url.Action("ClienteConfiguracaoPlano","ObtemContaPagamento")', { id: 21 }, function(data) { alert(data); });
... See if this is right and tell me.– Leandro Simões
Test so tbm:
$.get('/ClientConfiguracaoPlano/ObtemContaPagamento', { id: 21 }, function(data) { alert(data); });
– Leandro Simões
your last command worked... elaborate a response explaining how the route and parameters works.
– Marco Souza
Blz @Marconcilio Souza, I will prepare an answer and put here.
– Leandro Simões