I have a C# client that consumes a web service passing various parameters.
What I am 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 the code is done in jQuery how 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 I do trying to mimic 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>
Using jquery the generated code is completely different from c#
would you use
$.ajax
Call (Consume) Web Service (ASMX) using jQuery AJAX in ASP.Net
As you will see, the idea is to use a structure like this
In your case, in the parameter you
url
would$.ajax
indicate the/webservice.asmx/test
You can indicate in
data
it the information that you would send to the service parameters.