【问题标题】:Binding failed from a web service in asp.net从 asp.net 中的 Web 服务绑定失败
【发布时间】:2015-01-30 10:32:06
【问题描述】:

我有一个以 xml 数据返回的 Web 服务。它只需要一个参数,用户标识。它工作正常,直到我想从该 Web 服务中检索数据并附加到我的 C# asp.net 中。当我运行脚本时,它总是落入错误(“未找到贷款”),其中实际上有相同参数输入的结果。

我已经

  • 在我的脚本中硬编码。 (userid-'18450')
  • 在脚本的 content-type 中将结果设置为 json
  • 检查了我的网络服务的网址

但我不知道为什么它没有从 Web 服务获取数据。请帮忙。

我的网络服务代码:

  using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.Script.Serialization;
    using System.Web.Script.Services;

namespace WebAppTest2
{
    /// <summary>
    /// Summary description for WebService1
    /// </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 WebService1 : System.Web.Services.WebService
    {

        public class Record
        {
            public int loanid { get; set; }
            //public string daterequested { get; set; }
            public string name { get; set; }
            public string remark { get; set; }
            //public string loandisplayid { get; set; }
            public string status { get; set; } 
            public string nothing { get; set; }

        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod()]
        public List<Record> GetData(int userid)
        {

            SqlConnection con = new SqlConnection("Data Source=DIT-NB1260382;Initial Catalog=loansystem;Integrated Security=True");
            con.Open();

            SqlCommand cmd = new SqlCommand("Equipment_ListPendingApproval ", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@userid", userid);

            SqlDataReader dr = cmd.ExecuteReader();

            List<Record> Record = new List<Record>();

            while (dr.Read())
            {
                Record.Add(new Record()
                {
                    loanid = dr.GetInt32(0),
                    //daterequested = dr.GetString(1),
                    name = dr.GetString(2),
                    //loandisplayid = dr.GetString(3),
                    status = dr.GetString(4),                  
                    remark = dr.GetString(5),


                });
            }

            dr.Close();
            con.Close();
            return Record;
        }
    }
}

我的输出

我在 asp.net 中的脚本:

<script>
$(document).ready(function () {
    //var userid = JSON.parse(sessionStorage.getItem('userID'));
    var userid = JSON.parse('18450');
    alert(userid);
    $.ajax({

        type: "POST",
        url: "http://localhost:52928/WebService1.asmx/GetData",
        data: JSON.stringify({ userid: userid }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {

            var loans = response.d;


            $.each(loans, function (index, Record) {
                var loanid = this.loanid;
                //construct your html here
                var loanListDiv = "";
                loanListDiv += "<li>";
                loanListDiv += "<a href='requestCollectionPopup.html' data-rel='dialog' data-transition='pop' class='ui-btn ui-btn-icon-right ui-icon-carat-r'>";
                loanListDiv += "<span class='ui-li-count ui-body-inherit' style='border:none; color:#800080; background-color:transparent;'>";
                loanListDiv += Record.status;
                loanListDiv += "</span>";
                loanListDiv += Record.loanid;
                loanListDiv += "</a>";
                loanListDiv += "</li>";

                $("#loansAll").append(loanListDiv).trigger("create");
            });

        },

        error: function (response) {
            alert("No loans Found");
        }
    });
});
</script>

【问题讨论】:

  • 还添加标签 JavaScript、AJAX 和 jQuery
  • 你能把你的整个代码贴在这里

标签: javascript jquery asp.net ajax web-services


【解决方案1】:
  1. 看起来您的data 参数$.ajax() 方法无效。它被命名为userid,而不是getUserID,对。
  2. 还要调查 Chrome 开发工具 (F12) 中的“网络”选项卡,查看服务器对此 AJAX 请求的实际响应。
  3. 当您能够通过 GET 请求获得响应时,type“POST”看起来也很可疑

【讨论】:

  • 我已经对它们进行了相应的修改,但它不起作用。
  • 您的浏览器控制台和开发工具的网络选项卡中输出了什么?有什么错误吗?
【解决方案2】:

要从任何服务调用 Web 服务,您需要将该服务标记为脚本服务。将以下属性添加到 Web 服务并尝试。

[System.Web.Script.Services.ScriptService]     

查看以下链接

http://www.dotnetjalps.com/2010/08/calling-aspnet-web-service-from-jquery.html http://www.codeproject.com/Articles/529847/Calling-ASP-NET-WebService-from-jQuery

【讨论】:

  • 是的,我已经添加了。但仍然没有阳性结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
相关资源
最近更新 更多