Retrieve JSON data from Web Service using JavaScript in ASP.Net

maideen
 
on Jun 18, 2021 02:46 AM
1115 Views

Sir

How to retrieve json data using javascript using web API Url in asp.net

I have an issue with below code. Result not showing in json format when clicking "Submit Button" which calls java Fuction

Example Doc no : 900, 484, 923, 865

Please advice me

Thank you

Maideem

Below is my code

<div>
    <asp:Label ID="Label1" runat="server" Text="Enter Receipt No"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
<div>
    <asp:Button ID="BtnSubmit" runat="server" Text="Submit" OnClientClick="GetDetails()" />
</div>

 

<script type="text/javascript">
   function GetDetails(varCnNo) {
       var varCnNo = document.getElementById('<%= TextBox1.ClientId %>').value;
       alert(varCnNo);
       return true;
       const url = new URL("http://courier.silsnetwork.com/WebService.asmx/GetBizTrak_DOCNO?strDocNo=" & varCnNo);
       let headers = {
           "Content-Type": "application/json",
           "Accept": "application/json"
       };
       let body = {
           "docno": varCnNo //
       }
       fetch(url, {
           method: "POST",
           headers: headers,
           body: body
       })
           .then(response => response.json())
           .then(json => console.log(json));
    }
</script>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 21, 2021 06:26 AM

Hi maideen,

Refer below code.

HTML

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:Label ID="Label1" runat="server" Text="Enter Receipt No"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="BtnSubmit" runat="server" Text="Submit" OnClientClick="GetDetails()" />
<script type="text/javascript">
    function GetDetails() {
        PageMethods.GetDetail(document.getElementById("<%=TextBox1.ClientID%>").value,
            function (response) {
                debugger;
                alert(response);
            });
    }
</script>

Code

C#

[System.Web.Services.WebMethod]
public static Data[] GetDetail(string no)
{
    string json = (new System.Net.WebClient()).DownloadString("http://courier.silsnetwork.com/WebService.asmx/GetBizTrak_DOCNO?strDocNo=" + no);
    return JsonConvert.DeserializeObject<Data[]>(json);
}

public class Data
{
    public string docno { get; set; }
    public string docdate { get; set; }
    public string custcode { get; set; }
    public string custname { get; set; }
    public string MarketingExe { get; set; }
    public string Transporter { get; set; }
    public string cnno { get; set; }
    public string invNo { get; set; }
    public double amount { get; set; }
}

VB.Net

    <System.Web.Services.WebMethod>
    Public Shared Function GetDetail(ByVal no As String) As Data()
        Dim json As String = (New System.Net.WebClient()).DownloadString("http://courier.silsnetwork.com/WebService.asmx/GetBizTrak_DOCNO?strDocNo=" & no)
        Return JsonConvert.DeserializeObject(Of Data())(json)
    End Function

    Public Class Data
        Public Property docno As String
        Public Property docdate As String
        Public Property custcode As String
        Public Property custname As String
        Public Property MarketingExe As String
        Public Property Transporter As String
        Public Property cnno As String
        Public Property invNo As String
        Public Property amount As Double
    End Class

For more details refer below articles.

Calling server side function from JavaScript in ASP.Net

Read (Parse) JSON data from URL and display in ASP.Net GridView using C# and VB.Net