Sencha touch 为什么我的控制器被调用了两次?

Sencha touch 为什么我的控制器被调用了两次?,sencha-touch,asp.net-mvc-3,http-post,Sencha Touch,Asp.net Mvc 3,Http Post,这可能很简单,但我就是看不出来 我已经让我的应用程序将数据发布到我的Web服务()。Sencha Touch js代码如下所示 var submitCommunicateCard = function () { console.log(rpc.views.Contact.CommunicateCard.getValues()); Ext.Ajax.request({ url: WebService('GetInTouch', 'CommunicateCard'),

这可能很简单,但我就是看不出来

我已经让我的应用程序将数据发布到我的Web服务()。Sencha Touch js代码如下所示

var submitCommunicateCard = function () {
    console.log(rpc.views.Contact.CommunicateCard.getValues());
    Ext.Ajax.request({
        url: WebService('GetInTouch', 'CommunicateCard'), //http://webservice.example.com/GetInTouch/CommunicateCard
        method: 'post',
        params: {
            callback: 'foo', //temporary until I can better setup the callback.
            name: rpc.views.Contact.CommunicateCard.getValues().name,
            city: rpc.views.Contact.CommunicateCard.getValues().city
        }
    });
};
因为我需要“阻止”我的跨站点脚本问题,所以我不得不编写一个
ActionFilter
,添加适当的标题

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();

            HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");

            string rqstMethod = HttpContext.Current.Request.Headers["Access-Control-Request-Method"];
            if (rqstMethod == "OPTIONS" || rqstMethod == "POST")
            {
                HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
                HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Headers", "X-Requested-With, Accept, Access-Control-Allow-Origin");
            }
        }
    }
}
在我的控制器中,我从我的应用程序接收数据,如下所示

    [AllowCrossSiteJsonAttribute]
    public JsonpResult CommunicateCard(CommunicateCardModel communicateCardModel)
    {
        CommunicateCardModel cc = null;
        string rqstMethod = System.Web.HttpContext.Current.Request.Headers["Access-Control-Request-Method"];
        if (rqstMethod != "POST")
        {
            // Do stuff with the model


             return this.Jsonp(true);
        }
        else { 
            return this.Jsonp(false); 
        }
    }
您将看到,我必须输入
if(rqstMethod!=“POST”)
,因为“POST”中的模型为空,而“OPTIONS”中的模型为空

以下是正在传递的原始标头。。。(注意:这两个头成对传递…即:控制器被调用两次。)

第一次呼叫

选项/GetInTouch/CommunicateCard HTTP/1.1
主持人:webservice.example.com
推荐人:
访问控制请求方式:POST
来源:
用户代理:Mozilla/5.0(Macintosh;英特尔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,en;q=0.8
接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.3

第二次调用(请注意包含发布数据的底线(第一次调用中不包含该数据)
callback=foo&name=Chester&city=Toronto

POST/GetInTouch/CommunicateCard HTTP/1.1
主持人:webservice.example.com
推荐人:
内容长度:38
来源:
X-request-With:XMLHttpRequest
用户代理:Mozilla/5.0(Macintosh;英特尔Mac OS X 10_7_0)AppleWebKit/534.24(KHTML,像Gecko)Chrome/11.0.696.71 Safari/534.24
内容类型:application/x-www-form-urlencoded;字符集=UTF-8
接受:/
接受编码:gzip、deflate、sdch
接受语言:en-US,en;q=0.8
接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.3

callback=foo&name=Chester&city=多伦多


有没有办法防止多次呼叫我的控制器?(或者为什么我的控制器被调用了两次?

结果是通过从我的Sencha Touch应用程序中调用JSONP进行修复。
Ext.util.JSONP.request

//这是错误的,不要使用此代码进行JSONP调用 //这是对的
var submitCommunicateCard = function () {
    console.log(rpc.views.Contact.CommunicateCard.getValues());
    Ext.Ajax.request({
        url: WebService('GetInTouch', 'CommunicateCard'), //http://webservice.example.com/GetInTouch/CommunicateCard
        method: 'post',
        params: {
            callback: 'foo', //temporary until I can better setup the callback.
            name: rpc.views.Contact.CommunicateCard.getValues().name,
            city: rpc.views.Contact.CommunicateCard.getValues().city
        }
    });
var submitCommunicateCard = function () {
    console.log("Outbound Data Object:");
    console.log(rpc.views.Contact.CommunicateCard.getValues());
    Ext.util.JSONP.request({
        url: WebService('GetInTouch', 'CommunicateCard'),
        method: 'post',
        callbackKey: 'callback',

        params: {
            name: rpc.views.Contact.CommunicateCard.getValues().name,
            city: rpc.views.Contact.CommunicateCard.getValues().city
        },
        callback: function (result) {
            console.log("Inbound Data Object:");
            console.log(result);
            // Handle error logic
            if (result.success === true) {
                Ext.Msg.alert("Sent!", "Thank you, your message has been sent!", Ext.emptyFn);
                rpc.views.Contact.CommunicateCard.reset();
            } else {
                Ext.Msg.alert("Oops!", "looks like something went wrong, please try again.", Ext.emptyFn);
            }
        }
    });
};