javascript - 无法在javascript方法中传递ajax post

标签 javascript c# jquery ajax

当我读取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/

相关文章:

javascript - Windows 中使用 XMLHttpRequest 的 React.JS POST 值

javascript - 首次单击当前编辑行中的组合框时,Ext 4 RowEditor 编辑取消

javascript - 如何在另一个 SlideDown 时向上滑动另一个

javascript - jQuery 单选按钮表单默认

javascript - 如何向 KendoUI 数据源组件添加新记录

c# - 仅针对 webHttpBinding 更改 XML 和 JSON 中的响应输出格式

c# - 在字符串列表中查找子字符串

c# - WPF 选项卡控件 : How do I get the currently selected tab?

jquery - 将 jQuery 图标颜色更改为白色而不是灰色

jquery - 接收从loadFile发送到 Electron 中的html页面的数据