我有一个包含多个字段但作为此标题必不可少的表单:
<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 事件,我得到了 empty 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)
我仍然将所有值都清空!