[英]jQuery post method parameter passing fail
I am new to Javascript and learning POST method. 我是Java语言的新手,正在学习POST方法。 I wrote some code, but it doesn't work and I can't figure it out why. 我写了一些代码,但是它不起作用,我也不知道为什么。
Here is script code: 这是脚本代码:
<script>
$(document).ready(function () {
$.post("/api/totaluserexp",
{
startDate: '20150701',
endDate: '20150701'
},
function (data) {
document.write("Total Experience: " + data);
}, 'json');
});
</script>
Here is another script code, should do the same thing: 这是另一个脚本代码,应该做同样的事情:
<script>
$(document).ready(function () {
$.post("/api/totaluserexp",
{
startDate: "20150701",
endDate: "20150701"
})
.done(function (data) {
$(".result").html("Total Experience: " + data);
});
});
</script>
Both of them returns an empty page with no errors, no warnings etc. Even when I use alert , nothing happens. 他们两个都返回一个没有错误,没有警告等的空白页面。即使我使用alert ,也没有任何反应。
Here is my controller: 这是我的控制器:
public int Post(DateTime startDate, DateTime endDate)
{
return DBClassPackage.izamanraporlama.getUserTotalExp(startDate, endDate);
}
Set a variable in your controller to debug the input data and output result. 在控制器中设置变量以调试输入数据和输出结果。
public int Post(DateTime startDate, DateTime endDate)
{
int result = DBClassPackage.izamanraporlama.getUserTotalExp(startDate, endDate);
return result;
}
I figured it out. 我想到了。 I tried to return a simple integer (like return 5;) but it also didn't worked. 我试图返回一个简单的整数(如return 5;),但它也没有起作用。
I don't know why, but this Post method is not the Post method it should be. 我不知道为什么,但是这个Post方法不是应该的Post方法。 I managed to pass parameters in the url like this: 我设法像这样在url中传递参数:
<script>
$(document).ready(function () {
$.post("/api/totaluserexp/apiden?20150701&20150701",
{
// startDate: "20150701",
// endDate: "20150701"
})
.done(function (data) {
$(".result").html("Total Experience: " + data);
});
});
</script>
If somebody explain why, I will accept his/her answer, cause I am very curious. 如果有人解释为什么,我会接受他/她的回答,因为我很好奇。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.