请参阅以下代码
var pdf="xxxxxxx";
$.ajax({
type: "post",
url: "/Corporate/SampleChangeSummary/PDF",
data: JSON.stringify(pdf),
contentType: 'application/json',
dataType: 'json',
error: function (status, xhr) {
}
});
[HttpPost]
public ActionResult PDF(object pdf)
{
.......
}
但返回 pdf为空。问题是什么?我的代码中有任何语法错误吗?
答案 0 :(得分:1)
我认为contentType
是造成这个问题的原因。在你的那个剧本中,你告诉你的$.ajax()
它正在发送JSON字符串,但是你发送了一个.pdf。相反,只需删除contentType
行并尝试即可。
其次,dataType: 'json'
告诉您的$.ajax()
它正在期待JSON格式的响应。如果这就是你在做什么,那么你不必担心这一行。
编辑:
找到更多信息 - 如果您想保留contentType
,请修改为:contentType: application/pdf',
并在您的header('Content-type: application/pdf');
发送到的php文件中添加$.ajax()
。
答案 1 :(得分:0)
试试这个:
$.ajax({
type: "post",
url: "/Corporate/SampleChangeSummary/PDF",
data: JSON.stringify({pdf: pdf}),
contentType: 'application/json',
dataType: 'json',
error: function (status, xhr) { }
});
ASP.NET MVC要求您发送“键值”JSON对象,以便它可以相应地绑定。另外,将object pdf
更改为string pdf
。
答案 2 :(得分:0)
尝试这样的事情
$.ajax({
type: "POST",
url: '/Corporate/SampleChangeSummary/PDF',
data: { pdf: JSON.stringify(pdf) },
success: function (data) {
//do somthing with the result
},
error: function (error) {
alert('Please try again!');
}
});
答案 3 :(得分:0)
var pdf="xxxxxxx";
$.ajax({
type: "post",
url: "/Corporate/SampleChangeSummary/PDF",
data: { "pdf" : pdf },
dataType: 'json',
error: function (status, xhr) {
}
});
[HttpPost]
public ActionResult PDF(object pdf)
{
.......
}
您不需要对您的pdf进行字符串化,因为它已经是字符串尝试将其直接传递给您的控制器。