javascript - Ajax 调用不起作用这段代码有什么问题?

标签 javascript c# jquery ajax asp.net-mvc

这是我的看法

<td>  
@if (item.Flag == false)
{ 
   <a href="javascript:void(0)" id="@item.CustId" class="flag" onclick="flagCustomer('flase',this.id)">
   <img src="~/Images/appimg/star.png" alt="" /></a>
}
else
{   
    <a href="javascript:void(0)" class="flag" id="@item.CustId" onclick="flagCustomer('true',this.id)">
    <img src="~/Images/appimg/MapMarker_Flag.png" /></a>
}
</td>

这是我的 Ajax 调用

 function flagCustomer(flag, id) {
        debugger;
        var flag = flag;
        var id = id;
        debugger;
        $.ajax({
            type: "POST",
            url: "@Url.Action("Importantmark", "Customer")",
            data: { _flag: flag, _id: id },
            success: function (data) {
                if (data === "1") {
                    notif({ msg: "<b>Important Customer.", type: "success" });
                } else {
                    notif({ msg: "<b>error customer while important.", type: "error" });
                }
            },
            Error: function (data) {
                notif({ msg: "<b>error customer while important.", type: "error" });
            }
        });
        debugger;
    }

这是我的 Controller

[HttpPost]
public int Importantmark(string _flag, int _id)
{
    Customer _customer = new Customer();
    _customer.Flag = Convert.ToString(_flag);
    _customer.CustId = Convert.ToInt32(_id);
    var result = customerBal.ImportantMark(_customer);
    return result;
}

这是我的代码 当我提交点击这张图片时,我的 java 脚本函数被调用,但是当我调试代码时,它显示了 id 和 flag 属性。 成功调试器来到成功的属性,它走到了外面。这段代码有什么问题请解释一下

最佳答案

传递它应该工作的内容类型和数据类型

function flagCustomer(flag, id) {
        debugger;
        $.ajax({
            method: "POST",
            url: '@Url.Action("Importantmark", "Customer")',
            data: "{ '_flag': '" + flag+ "','_id': '" + id+ "' }",
            contentType: "application/json; charset=utf-8",
            async: true,
            dataType: "json",
            success: function (data) {
                if (data === "1") {
                    notif({ msg: "<b>Important Customer.", type: "success" });
                } else {
                    notif({ msg: "<b>error customer while important.", type: "error" });
                }
            },
            error: function (data) {
                notif({ msg: "<b>error customer while important.", type: "error" });
            }
        });
        debugger;
    }

关于javascript - Ajax 调用不起作用这段代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36099024/

相关文章:

jquery - 如何将边距设置为响应式 div?

javascript - 模态焦点在 div 元素上

javascript - Sails 政策 - 通过协会搜索

javascript - 在这种情况下使用间隔可以吗?

c# - 类库与其他项目的异常处理技巧

c# - AutoMapper 从源嵌套集合映射到另一个集合

javascript - 溢出时将内容移动到另一个 div

javascript - <输入类型 = 数字> 中的上级和下级

c# - HTML 敏捷包 - 解析表

javascript - iframe 自动高度在 Firefox 上无法正常工作