javascript - 从 asp.net 中的 Web 服务绑定(bind)失败

标签 javascript jquery asp.net ajax web-services

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

我已经

  • 将其硬编码到我的脚本中。 (userid-'18450')
  • 将结果设置为脚本中内容类型的 json
  • 检查了我的网络服务的网址

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

我的网络服务代码:

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

我的输出

enter image description here

我在 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>

enter image description here

最佳答案

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

关于javascript - 从 asp.net 中的 Web 服务绑定(bind)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28233899/

相关文章:

javascript - 默认 WebGL 颜色缓冲区示例

jQuery 循环插件 : Multiple Pagers for different galleries on same page

c# - response.write 不工作 asp.net

c++ - IIS-部署ASP.NET网站和数据库连接

javascript - 如何从 Node.js IMAP 模块中的正文获取纯文本

javascript - 使用 IATA 代码计算机场之间的距离

javascript - 离线网站如何保存数据?

javascript make select 2 使用外部源设置的 url

javascript - ".resize"事件与 't work with "偏移量“在一起

asp.net - 将 MS 图表导出为 PDF 和 Excel