将字符串值发送到 WebMethod

发布于 2024-10-16 04:41:23 字数 1238 浏览 5 评论 0原文

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。

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

Here is my WebMethod

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

When i send the empCode as '11479' 0r any integers from Ajax the web method is calling. when i send the empcode as "C1026" or any string with characters then the webmethod is not getting called..

Please suggest me how to pass empCode which is having string value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤云独去闲 2024-10-23 04:41:23

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

data: {'empCode': C1026}

您需要发送:

data: {'empCode': 'C1026'}

Put quotes around the EmpCode.Currently you are sending:

data: {'empCode': C1026}

You need to send:

data: {'empCode': 'C1026'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文