将字符串值发送到WebMethod

时间:2011-02-07 05:55:46

标签: asp.net asmx webmethod

function EmpCode(empCode) {
    var queryString = "";
    var hasQuerystring = document.URL.indexOf('?');
    if (hasQuerystring != -1) {
        queryString = document.URL.substring(hasQuerystring + 1, document.URL.length);
        if (queryString == 'id=1') {
            $.ajax({
                type: "POST",
                url: "EmployeeBasicInfo.aspx/CheckEmpCode",
                data: "{'empCode':" + empCode + "}",// Sending empCode which is a string
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    var errorMsg = (msg.d);
                    if (errorMsg != 'NA') {
                        alert(errorMsg);
                    }
                }
            });
        }
    }
}

这是我的WebMethod

[WebMethod]
public static  string CheckEmpCode(string empCode)
    {
    string empCod = "";
    EmployeeFactory empFactory = new EmployeeFactory();
    empCod = empFactory.CheckCode(empCode);
    return empCod;
}

当我发送empCode为'11479'0r来自Ajax的任何整数时,web方法正在调用。当我发送empcode为“C1026”或任何带字符的字符串时,webmethod就不会被调用..

请建议我如何传递具有字符串值的empCode。

1 个答案:

答案 0 :(得分:0)

在EmpCode周围加上引号。目前您正在发送:

data: {'empCode': C1026}

您需要发送:

data: {'empCode': 'C1026'}