用IE提交表单问题

时间:2012-08-05 21:29:48

标签: c# asp.net-mvc-3 cross-domain

我有一个包含多个字段的表单,但是这个标题必不可少:

<form action="http://api.domain_A.com/43bdb030-e1e4-4526-86e5-5b8ba98aa117/Form/Challenge" 
      method="post" 
      id="frm-challenge"
      accept-charset="utf-8" 
      enctype="application/x-www-form-urlencoded"> ... </form>

我正在提交:

var form = $(this).closest("form"),
    url = $(form).attr("action"),
    formdata = $(form).serialize();

if ($.browser.msie && window.XDomainRequest) {

    formdata = encodeURI(formdata);

    var xdr = new XDomainRequest();
    xdr.onload = submitAnswerDone;
    xdr.onerror = submitAnswerDone;
    xdr.open("POST", url);
    xdr.send(formdata);
}
else {
    $.support.cors = true;

    $.ajax({
        type: 'POST',
        url: url,
        data: formdata,
        success: submitAnswerDone,
        error: function (xhr, ajaxOptions, thrownError) {
            //console.log('result: Error ' + xhr.status);
            //console.log(thrownError);
        }
    });
}

表单托管在http://{client_name}.domain-A.com/,表单提交到http://api.domain-A.com/

在服务器端,我有一个 ASP.NET MVC 3 网站,它正在获取表单。

控制器操作如下:

[HttpPost]
public JsonResult Challenge(FormCollection form)
{
    // u = Calendar GUID
    // c = Challenge GUID
    // s = Subscriber GUID
    // a = Answer GUID

    // flash = Flash
    // screen = Screen

    StringBuilder data = new StringBuilder();

    foreach (string f in form)
        data.AppendFormat("Name: >{0}< Value: >{1}< |\n", f, form[f]);

    // ### LOG EVENT ###############################################
    try
    {

        Loggr.Events.Create()
            .Text("Challenge test [u]")
            .Source(Request.UrlReferrer != null ? Request.UrlReferrer.ToString() : Request.Url.ToString())
            .Data("Form: >{0}<\n, Request: >{1}<", form["u"], Request["u"])
            .AddData(data.ToString())
            .AddData("Browser: {0}", Request.ServerVariables["HTTP_USER_AGENT"])
            .Post();
    }
    catch (Exception ex)
    {
        Loggr.Events.Create()
            .Text("Error: Challenge test [u]")
            .Source(Request.UrlReferrer != null ? Request.UrlReferrer.ToString() : Request.Url.ToString())
            .Data(ex.Message)
            .AddData(data.ToString())
            .AddData("Browser: {0}", Request.ServerVariables["HTTP_USER_AGENT"])
            .Post();
    }

    // ### SAVE EVENT ###############################################
    SecondaryDb db = new SecondaryDb();
    try
    {
        string c, u, s, a, fl, sc;

        c = form["c"] ?? "";
        u = form["u"] ?? "";
        s = form["s"] ?? "";
        a = form["a"] ?? "";
        fl = form["flash"] ?? "";
        sc = form["screen"] ?? "";

        string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"],
                browser = Request.ServerVariables["HTTP_USER_AGENT"];

        if (string.IsNullOrWhiteSpace(ip))
            ip = Request.ServerVariables["REMOTE_ADDR"];

        db.AddSubscriberAnswerTest(u, c, a, s, browser, ip, fl, sc);

        string fields = String.Format("s:{0} c:{1} u:{2} a:{3}", s, c, u, a);

        return Json(new ApiResult() { ErrorCode = 0, Message = "Answer added (" + fields + ")" }, JsonRequestBehavior.DenyGet);

    }
    catch (Exception ex)
    {
        if (ex.InnerException != null)
            while (ex.InnerException != null)
                ex = ex.InnerException;

        StringBuilder fields = new StringBuilder();
        foreach (var f in form.AllKeys)
        {
            fields.AppendFormat("{0}:{1} | ", f, form[f]);
        }

        db.AddErrorTest(ex.Message, ex.StackTrace, fields.ToString().TrimEnd(' '));

        return Json(new ApiResult() { ErrorCode = 99, Message = ex.Message }, JsonRequestBehavior.DenyGet);
    }
}

问题在于,在所有浏览器中我都能正确地获取所有内容,但不能在任何 Internet Explorer 提交(7,8或9)

我解雇 Fiddler ,我可以看到表单已发送:

POST http://api.domain_A.com/43bdb030-e1e4-4526-86e5-5b8ba98aa117/Form/ChallengeTest HTTP/1.1
Accept: */*
Origin: http://preview.domain_A.com
Accept-Language: da-DK
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host: api.domain_A.com
Content-Length: 239
Connection: Keep-Alive
Pragma: no-cache

u=43bdb030-e1e4-4526-86e5-5b8ba98aa117&c=f83c28d0-112a-4a88-986f-bcae8b30113b&s=&testmode=true&cType=1&flash=Flash%253A+WIN+11%252C3%252C300%252C270&screen=Screen%253A+1920x1040+(24+bits)&preview=True&a=bc3eb9cc-29a2-43e9-b507-a39a417f53aa

我从中得到答案

HTTP/1.1 200 OK
Server: nginx
Date: Sun, 05 Aug 2012 21:12:43 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 78
Connection: keep-alive
Cache-Control: private
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Origin: *
X-UA-Compatible: IE=Edge,chrome=1
P3P: policyref="/w3c/p3p.xml", CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"

{"ErrorCode":0,"Message":"Answer added (s: c: u: a:)","Fields":"","Data":null}

并查看正在保存的Loggr事件,我变为空form["u"],换句话说,我的FormCollection为空!

我做错了什么?

跨域协议已经到位,您可以通过Fiddler Header输出进行检查,并且我已经crossdomain.xml

进行了对比
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">

<cross-domain-policy>
    <site-control permitted-cross-domain-policies="all"/>
    <allow-access-from domain="*" secure="false"/>
    <allow-http-request-headers-from domain="*" headers="*" secure="false"/>
</cross-domain-policy>

如果我从

改变
[HttpPost]
public JsonResult Challenge(FormCollection form)

[HttpPost]
public JsonResult Challenge(
     string u, string c, string s, string a, string flash, string screen)

我仍然将所有值都清空!

1 个答案:

答案 0 :(得分:0)

尝试通过请求获取表单属性

Request.Form["u"]