是什么决定了“;成功;或“;错误“;函数被调用
What determines whether the "success" or "error" function is called?
我一直不明白:是什么决定了的success
还是error
功能
$.ajax( { ...,
success : function ( retobj ) { ... },
error : function ( retobj ) { ... },
...
} );
被称为?我的控制器可以直接控制调用哪个吗?我知道如果我的控制器做了一些愚蠢的事情,它会被调用,但我能强迫它像一样被调用吗
$.ajax( { ...,
url : 'MyController/CallSuccess',
success : function ( retobj ) { /* this will invetiably be called */},
error : function ( retobj ) { ... },
...
} );
public ActionResult CallSuccess ( void )
{
// ...
}
您的控制器操作方法可以通过在操作方法中设置Response.StatusCode
来控制是调用success
还是error
ajax
函数。
如果是Response.StatusCode = (int)HttpStatusCode.OK
,则将调用success
函数。
如果是Response.StatusCode = (int)HttpStatusCode.InternalServerError
,则将调用error
函数。
调用success
函数的示例代码:
$.ajax({
url: 'MyController/CallSuccess',
success: function(result) { /* this will be called */
alert('success');
},
error: function(jqXHR, textStatus, errorThrown) {
alert('oops, something bad happened');
}
});
[HttpGet]
public ActionResult CallSuccess()
{
Response.StatusCode = (int)HttpStatusCode.OK;
return Json(new { data = "success" }, JsonRequestBehavior.AllowGet);
}
调用error
函数的示例代码:
$.ajax({
url: 'MyController/CallFailure',
success: function(result) {
alert('success');
},
error: function(jqXHR, textStatus, errorThrown) { /* this will be called */
alert('oops, something bad happened');
}
});
[HttpGet]
public ActionResult CallFailure()
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return Json(new { data = "error" }, JsonRequestBehavior.AllowGet);
}
相关文章:
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 如何在Javascript函数调用中循环变量
- Javascript:应为赋值或函数调用,但实际看到的却是表达式
- 如何远程检查JavaScript应用程序的函数调用堆栈
- javascript函数调用不起作用
- 为什么这个函数调用会破坏程序并导致未定义的变量
- 如何通过函数调用设置图像的src
- 从全局函数调用Ember控制器上的方法
- 为什么Jquery$.ajax在函数调用中触发所有statusCode,即使调用成功
- JavaScript函数调用(arg1)(arg2)
- 打印链接时,将javascript函数调用到链接中
- 在函数调用中封装数据除了隐藏数据之外还有什么优点
- 无法从JavaScript中的函数调用对象属性
- 对中的函数调用进行排序是回调的唯一方法
- 函数调用方法有什么用
- Javascript:JSHint:应为赋值或函数调用,但实际看到的却是表达式
- HTML5(Bootstrap)通过函数调用运行动画
- 函数调用不起作用
- 函数中的Javascript函数调用