当我读取html图像值并在java脚本方法中传递值数据时,我遇到了一个小冲突,并且在ajax POST中将值传递给 Controller 时,不幸的是ajax将 Controller 指向断点,但是字符串 Country 参数为 null(传递 null 值=),我可以知道原因吗!谢谢 !
public ActionResult Prayer_Schedule(string Country){
}
<img src="~/images/flags/india.jpg" value="india" onclick="choose(this);">
<script type="text/javascript">
function choose(element) {
var Country = element.getAttribute("value");
$.ajax({
type: 'POST',
url: '../Home/Prayer_Schedule',
data: {
Country: Country,
},
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
cache: false,
timeout: 100000,
success: function (response) {
alert('ajax success: ' + response);
//location.href = "/thankyou.html";
}
});
最佳答案
您必须使用jQuery.param对于帖子
data: jQuery.param({ Country: Country}),
或者使用$.post
$.post('../Home/Prayer_Schedule', { Country: Country },
关于javascript - 无法在javascript方法中传递ajax post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41457805/