【问题标题】:Not able to get data from web service using json asp.net无法使用 json asp.net 从 Web 服务获取数据
【发布时间】:2015-01-23 11:45:19
【问题描述】:

我的项目中有一个 Web 服务,并使用 jquery 从另一个页面调用它。 但是没有从 Web 服务获取数据,我已经在 Web 服务中使用断点检查它没有受到 jquery 调用的影响。我的代码是。

网络服务

namespace DOC_HTML
{
/// <summary>
/// Summary description for JsonService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class JsonService : System.Web.Services.WebService
{

    [WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
    public static string DocTypeList()
    {
        MastersService MS = new MastersService();   
        DataSet ds = new DataSet();
        ds.ReadXml(new XmlNodeReader(MS.DocumentTypeList()));

        List<dropDown> listDocType = new List<dropDown>();

        if (ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dropDown objst = new dropDown();
                objst.Id = Convert.ToString(ds.Tables[0].Rows[i]["docCode"]);
                objst.Name = Convert.ToString(ds.Tables[0].Rows[i]["docName"]);
                listDocType.Insert(i, objst);
            }

        }
        JavaScriptSerializer jscript = new JavaScriptSerializer();
        return jscript.Serialize(listDocType);
    }

}

public class dropDown
{
    public string Id { get; set; }
    public string Name { get; set; }
}
}

网页代码..

<script type="text/javascript">     

    $(document).ready(function () {
        loadData();
    });

    function loadData() {  
            $.ajax({
                type: "POST",
                url: "../JsonService.asmx/DocTypeList",
                data: {},
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    var jsdata = JSON.parse(data.d);
                    $.each(jsdata, function (key, value) {
                        $('#<%= ddlDocType.ClientID %>').append($("<option></option>").val(value.Id).html(value.Name));
                });
            },
            error: function (e) {
                alert(JSON.stringify(e));
            }
        });          
    }

</script>

【问题讨论】:

  • 线索在回复中:Unknown web method DocTypeList。有没有调试过找不到方法的原因?
  • 由于您没有将任何数据传递给您的网络服务,请删除该行 contentType: "application/json; charset=utf-8", 。有时这也会导致问题。
  • 我试过不工作。
  • @RoryMcCrossan 如何调试它。
  • 得到了解决方案,我刚刚从 web 方法中删除了静态,它可以工作....

标签: jquery asp.net json web-services


【解决方案1】:

看看这个Stack Overflow 问题。我怀疑您的 JQuery ajax 调用正在寻找一种接受 POST 的方法,但可能没有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2018-07-03
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    相关资源
    最近更新 更多