我对用于处理来自支付网关的请求的控制器进行了操作。他们将请求正文中的 json 发送到我的服务器上执行此操作的 url。当他们发送数据时,此代码运行良好:
[ValidateInput(false)]
public ActionResult WebHookHandler()
{
var json = new StreamReader(Request.InputStream).ReadToEnd();
if (string.IsNullOrEmpty(json))
return new HttpStatusCodeResult(400); // bad request
...
// return ok status
return new HttpStatusCodeResult(200); // ok
}
问题是我想通过向它提交测试数据来测试这个动作,但我无法让它工作。我尝试的所有结果都会导致 400 响应,这意味着我发送的 json 没有在服务器端提取。我认为 MVC 3 试图变得太聪明,并且正在处理我以不允许从Request.InputStream属性中检索它的方式发送它的 json,因为该属性在任何 ajax 配置上始终为空我'我试过了。
我已经尝试了各种组合,例如对数据进行字符串化、将 processData 设置为 false、不同的 contentType,并且没有什么能让数据以允许我的服务器端代码从Request.InputStream中获取它的方式通过。
这是我的 javascript 的样子:
var data = $("#stripeJSON").val();
$.ajax({
url: "http://localhost/PaymentStripe/WebHookHandler",
type: "POST",
data: data,
processData: true,
contentType: "application/json",
success: function (data, textStatus, jqXHR) {
$("#result").html("success");
},
error: function (jqXHR, textStatus, errorThrown) {
$("#result").html("failed:<br/>" + textStatus + errorThrown);
},
complete: function (jqXHR, textStatus) {
}
});
这是一些虚拟的 json 数据:
{
"pending_webhooks": 1,
"type": "invoice.payment_succeeded",
"object": "event",
"created": 1347318097,
"livemode": false,
"id": "evt_0LMvt7Q9vL1oFI",
"data": {
"object": {
"currency": "usd",
"ending_balance": null,
"customer": "cus_0LMvSw8LEmOcJG",
"discount": null,
"id": "in_0LMvHGx1XutT7p",
"object": "invoice",
"amount_due": 0,
"date": 1347318097,
"total": 0,
"subtotal": 0,
"charge": null,
"period_end": 1347318097,
"next_payment_attempt": null,
"livemode": false,
"attempted": true,
"period_start": 1347318097,
"starting_balance": 0,
"lines": {
"subscriptions": [{
"quantity": 1,
"period": {
"end": 1349910097,
"start": 1347318097
},
"amount": 0,
"plan": {
"livemode": false,
"trial_period_days": null,
"amount": 0,
"object": "plan",
"name": "ZeroMonthly",
"id": "ZeroMonthly",
"interval_count": 1,
"currency": "usd",
"interval": "month"
}
}],
"prorations": [],
"invoiceitems": []
},
"paid": true,
"closed": true,
"attempt_count": 0
}
}
}
建议?