简体   繁体   English

从asp.net中的Web服务获取数据后出现“未定义”结果

[英]“undefined” result after getting data from web service in asp.net

I'm sorry to bother you but I need help with this. 很抱歉打扰您,但我需要帮助。 I want to get data back from Web Services in asp.net . 我想从asp.net中的Web服务获取数据。 I have called the service from the next JQuery: 我已经从下一个JQuery调用了该服务:

        $(document).ready(function () {
             $("#btn").click(function () {

            var name = "";
            $.ajax({
                type: "POST",
                contenttype: "application/json; charset=utf-8",
                data: "{null}",
                url: "EmployeeInfo.asmx/GetEmployeeInfo",
                dataTyp: "json",
                success: function (res) {
                    name = res.d;
                    alert(name);

                },
                error: function (err) {
                    alert(err);
                }
            });


        }); 
    });          

I will get Employee name from DB but the next code is just for testing: 我将从数据库获得Employee名称,但是下一个代码仅用于测试:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetEmployeeInfo()
    { 
        return "John";
    }

The problem is I'm getting "undefined" as a result from the retrieved value. 问题是从检索到的结果中得到“未定义”。 am I missing something? 我错过了什么吗?

Thank you all for your efforts and help 谢谢大家的努力和帮助

$(document).ready(function () {
    $("#btn").click(function () {
        var name = "";
        $.ajax({
            type: "POST",
            url: "*youepage.aspx*/GetEmployeeInfo",
            data: "",
            contentType: "application/json",
            dataType: "json",
            success: function (response) {
                name = response.d;
                alert(name);
            },
            error: function (response) {
                alert(response);
            }
        });
    }); 
});    

For testing: in the same page (your .aspx) go to code behind and write: 测试:在同一页(您的.aspx)中,转到后面的代码并编写:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static string GetEmployeeInfo()
    {
        return "jack";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM