我的Sencha Touch应用正在将表单发布到我的 WebService上,但不是发送POST而是发送OPTIONS。
POST
OPTIONS
我在这里读取了类似的线程,但是我只是不知道如何OPTIONS在代码中处理该方法。
我确实尝试将[AllowAjax]属性添加到Action中,但是MVC3中似乎不存在该属性。
[AllowAjax]
选项/ GetInTouch / CommunicateCard HTTP / 1.1 主机:webservice.example.com 引用 网址 :http://192.168.5.206/ Access-Control-Request-Method:POST 来源:http://192.168.5.206 用户代理:Mozilla / 5.0 (Macintosh; Intel Mac OS X 10_7_0)AppleWebKit / 534.24(KHTML,例如Gecko)Chrome / 11.0.696.71 Safari / 534.24 访问控制请求标头:X-Requested-With,内容类型 接受: / 接受编码: gzip,deflate,sdch 接受语言:en-US,zh; q = 0.8 接受字符集:ISO-8859-1,utf-8; q = 0.7,*; q = 0.3
在我的ActionMethod中,我正在使用以下代码。
public JsonpResult CommunicateCard(CommunicateCard communicateCard) { // Instantiate a new instance of MailMessage MailMessage mMailMessage = new MailMessage(); // removed for security/brevity // Set the body of the mail message mMailMessage.Body = communicateCard.name; // THIS IS CURRENTLY BLANK :-( // removed for security/brevity mSmtpClient.Send(mMailMessage); // do server side validation on form input // if it's valid return true // else return false // currently returning NULL cuz I don't care at this point. return this.Jsonp(null); }
原来我必须创建一个 ActionFilterAttribute
ActionFilterAttribute
namespace WebService.Attributes { public class AllowCrossSiteJsonAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); HttpContext.Current.Response.Cache.SetNoStore(); filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*"); string rqstMethod = HttpContext.Current.Request.Headers["Access-Control-Request-Method"]; if (rqstMethod == "OPTIONS" || rqstMethod == "POST") { filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Headers", "X-Requested-With, Accept, Access-Control-Allow-Origin, Content-Type"); } base.OnActionExecuting(filterContext); } } }