简体   繁体   English

从ASP.NET中的Web服务绑定失败

[英]Binding failed from a web service in asp.net

I have a web service returning in xml data. 我有一个返回xml数据的Web服务。 It only requires one parameter, the userid. 它仅需要一个参数userid。 It's working fine, until when I want to retrieve data from that web service and append in my C# asp.net. 一切正常,直到我想从该Web服务检索数据并附加到我的C#asp.net中为止。 When I run the script, it always fall to error-("no loan found") where there is actually result with the same parameter input. 当我运行脚本时,它总是会落在错误-(“未找到借项”)上,在该情况下,使用相同的参数输入实际上会得到结果。

I have already 我已经

  • Hardcoded it in my script. 在我的脚本中对其进行硬编码。 (userid-'18450') (用户ID-'18450')
  • Set result to json at content-type in the script 在脚本的content-type处将结果设置为json
  • Checked the url of my web service 检查了我的网络服务的网址

But I have no idea why is it not getting data from the web service. 但是我不知道为什么它不能从Web服务获取数据。 Please help. 请帮忙。

My web service code: 我的网络服务代码:

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

My output 我的输出

在此处输入图片说明

My script in the asp.net: 我在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>

在此处输入图片说明

  1. looks like your data parameter of $.ajax() method is invalid. 看起来$.ajax()方法的data参数无效。 It's named userid , not getUserID , right. 命名为userid ,而不是getUserID
  2. Also investigate Network tab in Chrome dev tools ( F12 ) and see what actually server responds for this AJAX request. 还要研究Chrome开发者工具( F12 )中的“网络”标签,并查看服务器对该AJAX请求的实际响应。
  3. Also type "POST" looks suspicious, when you are able to get response with GET request 当您能够通过GET请求获得响应时,还type “ POST”看起来可疑

To call web service from any service you need to mark that service as script service. 要从任何服务调用Web服务,您需要将该服务标记为脚本服务。 Add following attribute to web service and try. 将以下属性添加到Web服务,然后尝试。

[System.Web.Script.Services.ScriptService]     

See following links 见以下链接

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 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的

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

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