How to consume a web service in jQuery

1

I have a client in C # that consumes a web service by passing several parameters.

What I'm looking to do is translate this call that is made in C # and make it in jQuery.

I have searched several forums but the information is not complete.

Could you tell me how to translate the following code and explain to me once this fact the code in jQuery as it is executed?

Thank you very much

protected void Page_Load(object sender, EventArgs e)
{
dynamic myURL;
dynamic myKey;
dynamic myBlockURL;
dynamic myUser;


//TODO: Change the following based on your BPI information.
myURL = "http://localhost/test/webservice.asmx/test";
myKey = "BIA38KRVQ6V2MQnvmUnC";
myBlockURL = "http://localhost/test/test.aspx?blockid=_zu&culture=&userid=";

//TODO: Change the following to the username of the person logged in
myUser = "User1";

// Create a server object to use to encrypt the user ID
dynamic objHTTP;
objHTTP = Interaction.CreateObject("WinHttp.WinHttpRequest.5.1");
objHTTP.open("POST", myURL);
objHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


 /*Construct a URL with a query string to encrupt the user ID*/
 dynamic strPost;
 strPost = "sourcetxt=" + myUser + "&Key=" + myKey;

 // Construct a URL with a query string to encrupt the user ID
 strPost = null;
 strPost = "sourcetxt=" + myUser + "&Key=" + myKey;

 // Perform the query
 objHTTP.Send(strPost);

 // Retrieve the encrypted user ID from the result of the query
 dynamic encryptedID = null;
 if (objHTTP.statusText != "Unauthorized" & objHTTP.statusText != "Not Found")
 {
     dynamic oXMLDoc = null;
     oXMLDoc = Interaction.CreateObject("MSXML2.DOMDocument");
     oXMLDoc.loadXML(objHTTP.ResponseText);
     encryptedID = oXMLDoc.getElementsByTagName("string")(0).childNodes(0).text;
     objHTTP = null;
 }

 // Output some HTML with an iFrame, the BPI block URL and the encrypted user ID
 Response.Write(" <iframe src='" + myBlockURL + encryptedID + "' id='Test' scrolling='yes' width='900' height='400'></iframe>");
}

Here is the code that I do trying to imitate in ajax:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("[id*=btnSubmit]").click(function () {
            var myURL = $.trim($("[id*=myURL]").val());
            var myKey = $.trim($("[id*=myKey]").val());
            var myBlockURL = $.trim($("[id*=myBlockURL]").val());
            var myUser = $.trim($("[id*=myUser]").val());

            $.ajax({
                type: "POST",
                url: "http://localhost/test/webservice.asmx/test",
                data: "{ myURL: '" + myURL + "', myKey: " + myKey + ",myBlockURL:" + myBlockURL + ",myUser:" + myUser + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    alert(r.d);
                },
                error: function (r) {
                    alert(r.responseText);
                },
                failure: function (r) {
                    alert(r.responseText);
                }
            });
            return false;
        });
    });
</script>
    
asked by A arancibia 26.04.2016 в 21:22
source

1 answer

1

Using jquery the generated code is completely different from that of c #

Usarias $.ajax

Call (Consume) Web Service ( ASMX) using jQuery AJAX in ASP.Net

As you will notice, the idea is to use a structure like this

   $.ajax({
        type: "POST",
        url: "Service.asmx/GetDetails",
        data: "{ name: '" + name + "', age: " + age + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {
            alert(r.d);
        },
        error: function (r) {
            alert(r.responseText);
        },
        failure: function (r) {
            alert(r.responseText);
        }
    });

In your case in the parameter url of $.ajax you would indicate the /webservice.asmx/test

You can indicate in the data the information that you would send to the service parameters.

    
answered by 26.04.2016 / 21:29
source