locked
Call Webservice from JS, sending some data RRS feed

  • Question

  • User1378609846 posted

    hi,

    I have a webservice created with asp, which is:

    Imports System
    Imports System.IO
    Imports System.Text
    Imports System.Web
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    
    <System.Web.Script.Services.ScriptService()>
    <WebService(Namespace:="http://tempuri.org/")>
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
    Public Class WBS_Actions
        Inherits System.Web.Services.WebService
    
        <WebMethod()>
        Public Sub TrackAction(byval sText as string)
            Dim path As String = "c:\MyTest.txt"
            Using fs As FileStream = File.Create(path)
                Dim info As Byte() = New UTF8Encoding(True).GetBytes(sText)
                ' Add some information to the file.
                fs.Write(info, 0, info.Length)
            End Using
        End Sub
    
    End Class

    On anoter website, I have a JS that calls this webservice like this:

    function ActionDone() {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://localhost:57358/WBS_Actions.asmx/TrackAction', true);
        xhr.setRequestHeader('Content-type', 'text/plain');
        xhr.send("test");    
    }

    Now, the issue is that the file is not created with that code. Instead, if I changed the webservice to the code below, the file is created, but I am missing the option to send data from JS to the webservice.

    Public Sub TrackAction()
            Dim path As String = "c:\MyTest.txt"
            Using fs As FileStream = File.Create(path)
                Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is my text")
                ' Add some information to the file.
                fs.Write(info, 0, info.Length)
            End Using
    End Sub

    Any help would be much appreciated, thanks!

    imendimu

    Tuesday, February 13, 2018 11:29 PM

Answers

  • User1168443798 posted

    Hi,

    I suggest you make a test with code below:

    function ActionDone() {
    	alert("test_0");
        
            alert("test_1");
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://localhost:57358/WBS_Actions.asmx/TrackAction', true);
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            alert("test_2");
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    alert("test_3");
                    console.log(xhr.responseText);
                }
            }
            xhr.send("sText=" + 'test');
    }

    Best Regards,

    Edward

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 1:23 PM

All replies

  • User1168443798 posted

    Hi imendimu,

    For your original method with parameter, I suggest you try code below:

       $(document).ready(function () {
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://localhost:61275/WBS_Actions.asmx/HelloWorld', true);
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    console.log(xhr.responseText);
                }
            }
            xhr.send("sText=" + 'test');
    
        })

    Best Regards,

    Edward

    Thursday, February 15, 2018 8:33 AM
  • User1378609846 posted

    Hi,

    Adding your suggested code to the function called in the onClick of the control, nothing happens.

    function ActionDone() {
    	alert("test_0");
        $(document).ready(function () {
            alert("test_1");
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://localhost:57358/WBS_Actions.asmx/TrackAction', true);
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            alert("test_2");
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    alert("test_3");
                    console.log(xhr.responseText);
                }
            }
            xhr.send("sText=" + 'test');
    
        })
    }

    Only the first alert("test_0"); is shown.

    Just to remind that these are two different solutions, and I test them running both of them at the same time, obviously.

    Thanks

    Thursday, February 15, 2018 1:27 PM
  • User1168443798 posted

    Hi,

    I suggest you make a test with code below:

    function ActionDone() {
    	alert("test_0");
        
            alert("test_1");
            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'http://localhost:57358/WBS_Actions.asmx/TrackAction', true);
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            alert("test_2");
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    alert("test_3");
                    console.log(xhr.responseText);
                }
            }
            xhr.send("sText=" + 'test');
    }

    Best Regards,

    Edward

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 19, 2018 1:23 PM
  • User1378609846 posted

    That works! Many thanks!

    Monday, February 19, 2018 3:10 PM