jQuery .post() and dynamically specifying the return type
-
23-09-2019 - |
Question
I have the following $.post() function that expects JSON as its return value.
$.post($(this).attr("action"), $(this).serialize(), function(data)
{
if (data.returnData)
{
//do stuff with data.returnData
}
}, "json");
This piece of code does the trick so long as the return type is JSON.
The question I have though, is whether it's possible to dynamically determine/specify the return type? The reason for this is that I have ActionMethods that could return Json or some other datatype (e.g. html or a redirect), and this $.post() function fails unless it's JSON.
Any suggestions?
Thanks
Dave
Solution
You could use $.ajax instead of the simpler $.post, and then figure out what the response body is with your own code.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow