Ajax调用无法正常运行此代码有什么问题?

时间:2016-03-19 07:21:06

标签: 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;
    }

这是我的控制器

[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属性。 成功调试成功的财产它走了出去。这段代码有什么问题请解释我

3 个答案:

答案 0 :(得分:3)

传递应该有效的contenttype和数据类型

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;
    }

答案 1 :(得分:0)

可能是你的报价:

url: '@Url.Action("Importantmark", "Customer")',

还需要添加内容类型,而不需要重新启动flagid,因此会:

function flagCustomer(flag, id) {
        debugger;
        $.ajax({
            type: "POST",
            url: '@Url.Action("Importantmark", "Customer")',
            data: { _flag: flag, _id: id },
            contentType: "application/json; charset=utf-8",
            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;
    }

答案 2 :(得分:0)

首先更改以下行:

<a href="javascript:void(0)" id="@item.CustId" class="flag" onclick="flagCustomer('flase',this.id)">

要:

<a href="javascript:void(0)" id="@item.CustId" class="flag" onclick="flagCustomer('false',this.id)"> // flase to false
  

检查它是否有效:

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