I have this piece of code:
function MyFunction()
{
$.ajax({
type: "POST",
url: "ajax.php",
dataType: "json",
data: "foo=bar",
error:function(XMLHttpRequest, textStatus, errorThrown)
{
alert(arguments.callee);
},
success: function(jsonObject)
{
//do something
}
});
}
what I want is that the alert inside de error scoope shows the function name, in this case “MyFunction” but instead what I get is the error:function.
How can I achieve this?
This –
is what you need.
The
argumentsinside theerrorfunction refers to this method’s ownargumentsobject. It does not refer to the MyFunction’sargumentsobject. That’s why you are gettingerror:MyFunction. Using a global variable in this case provides you a workaround to this problem.Also, to get only the name of the function, you need to use
arguments.callee.name.arguments.calleewill give you a reference to the calling function, not a function name in string.