har*_*ino 7 asp.net-mvc jquery
我有一个控制器动作,它在数据库中做了一些工作,然后在它完成时退出.这个动作是通过jQuery的ajax函数调用的,dataType设置为'json'.
如果我将操作的返回类型设置为void,一切都会正常运行,除非Firefox在控制台中显示错误:"找不到元素".
如果期望XML回来,Firefox会抛出这个错误是有道理的.但是,即使我将ajax调用的dataType属性更改为"text",我仍然会收到错误.为了摆脱返回类型为void的错误,我必须将Response的ContentType设置为"text/html".或者我可以将返回类型设置为JsonResult并返回一个新的[empty] JsonResult对象.
我确信有几种方法可以使这个错误消失,但我想知道处理动作的正确方法,没有通过ajax调用返回值.
如果重要,我也使用异步控制器动作模式.
public void DoSomethingAsync(SomeJsonObjectForModelBinding model)
{
// do some database things
}
public void DoSomethingCompleted()
{
// nothing to do...
// what should my return type be?
// do I need to set the content type here?
}
Run Code Online (Sandbox Code Playgroud)
Kon*_*Kon 11
我知道这并没有完全回答你的问题,但我认为你应该总是从AJAX或Web服务调用返回一个返回值.即使只告诉您操作成功,否则将错误(消息)返回给您.
我经常定义这样一个类:
public class JsonResultData
{
private bool _success = true;
public bool Success
{
get { return _success; }
set { _success = value; }
}
public object Value { get; set; }
public List<string> Errors { get; set; }
public JsonResultData()
{
this.Errors = new List<string>();
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用它在JsonResultData包装器中返回数据或任何其他调用元数据,如下所示:
return new JsonResult {
Data = new JsonResultData { Value = returnValue, Success = true }
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4187 次 |
| 最近记录: |