[英]unable to pass ajax post in java script method
我在讀取html圖像值並在java腳本方法中傳遞值數據時遇到了小沖突,並且在ajax POST中將值傳遞給控制器,但不幸的ajax將控制器指向斷點,但字符串為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 },
您已經設置了contentType: 'application/json; charset=utf-8'
contentType: 'application/json; charset=utf-8'
因此您需要發送json object
,也可以簡單地將其刪除以在控制器中獲取值。
$.ajax({
type: 'POST',
url: '../Home/Prayer_Schedule',
data: {
Country: Country,
},
dataType: 'json',
async: false,
cache: false,
timeout: 100000,
success: function (response) {
alert('ajax success: ' + response);
//location.href = "/thankyou.html";
}
});
另外,如果您還沒HttpPost
裝飾您的method
,那么您也將需要它。
[HttpPost]
public ActionResult Prayer_Schedule(string Country){
}
它可能是您在getAttribute()函數上的javascript。 您正在使用值,該值不是img標簽的有效屬性。 使用data- *屬性嘗試以下操作:
<img src="~/images/flags/india.jpg" data-value="india" onclick="choose(this);">
然后在您的JavaScript中:
element.getAttribute("data-value");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.