6
I’ve always been used to using $.ajax()
for all my requests of the type, and this works perfectly. An example that is even occurring now is the following:
$.ajax({
type: "POST",
url: "XmlTree.aspx/GetChildren",
data: {id : teste},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (callback) {
callback(msg);
}
},
error: function (msg) {
if (callback){
callback(msg);
}
}
});
Calling so, it falls into the method I intended. Everything works right as the image below shows:
However, I did read about the $.post
and your syntax seemed to me much simpler, and I decided to test, in the following way:
$.post( "XmlTree.aspx/GetChildren", { id : "S1000" } );
the problem is that regardless of my parameters, using $.post
it always falls in Page_load, as shown in the image:
I am aware of the existence of this question: What are the advantages of using the right HTTP methods?.
But still I could not understand the reason for this behavior. The $.post
should also not fall directly into my method GetChildren
?
Using the function
$.post
you need to serialize the data on your own. Try ordering the address and pass a string as parameter ->$.post( "XmlTree.aspx/GetChildren", JSON.stringify({id : "S1000" }) );
– Jéf Bueno
@LINQ he accepts a
PlainObject
also.– Woss
@Andersoncarloswoss But it will probably send as formdata and the backend will not deserialize it
– Jéf Bueno
Artur, this is Webforms?
– Jéf Bueno
@LINQ tried to pass the data as a string, but still fell into Load :/. Yes, it’s Webforms
– Artur Trapp
You would need to see how are the two requests, have something that differentiates the two and makes the backend treat them differently.
– Jéf Bueno
@LINQ went to analyze the
payload
in the Network tab of Chrome devtools, and although both are equal, the$.post
appears as Formdata, even though I put it in string form, and the$.ajax
string.– Artur Trapp
@Arturotemplario Apparently you have no choice, $.post will always send a formdata.
– Jéf Bueno
@LINQ understood, so I assume the situations in which the
$.post
can be used correctly are answered in the question I mentioned– Artur Trapp