2
我在.Net中有一個針對學校的項目。 我試圖通過AJAX發送POST JSON一個MVC控制器5.我到控制器的正確的功能,但它收到的值是0或空: values in controller are 0 or null使用Ajax發送數據給控制器[Project .Net]
儘管變量正確填寫瀏覽器控制檯: variables in browser console
您認爲該錯在哪裏?
下面是一些代碼: 功能調用控制器:
function GeneratePDF() {
var dataToSend;
var age = 69;
instruction = "trololololoCOCO";
dataToSend = JSON.stringify({ item: { distance: age, instruction: instruction } });
console.log("dataToSend : " + dataToSend);
var uri = '/GeneratePDF/Print';
$.ajax({
type: 'POST',
url: uri,
data: dataToSend,
contentType: "application/json; charset=utf-8",
})
.done(function (data) {
console.log('pdf controller DONE');
})
.fail(function (jqXHR, textStatus, err) {
console.log(jqXHR);
$("#result").html(jqXHR.responseText);
});
}
控制器:
[HttpPost]
public ActionResult Print(test item)
{
//something
return new Rotativa.ActionAsPdf("ViewPrint",new { id = item }) { FileName = "Cartographie.pdf" };
}
型號:
public class test
{
public int distance;
public string instruction;
public test(int distance, string instruction)
{
this.distance = distance;
this.instruction = instruction;
}
}
感謝了很多,它的工作原理。 –
@ M.Vogeleer當它解決你的問題時,不要忘記接受/ upvote答案! –