我有一个 C# 客户端,它使用一个传递各种参数的 Web 服务。
我要做的是翻译这个用 C# 制作的调用并用 jQuery 制作。
我搜索了几个论坛,但信息不完整。
您能否告诉我如何翻译以下代码并在 jQuery 中完成代码后向我解释它是如何执行的?
非常感谢
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>");
}
这是我试图在 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>
使用jquery生成的代码与c#完全不同
你会用吗
$.ajax
在 ASP.Net 中使用 jQuery AJAX 调用(使用)Web 服务 (ASMX)
正如您将看到的,我们的想法是使用这样的结构
在您的情况下,在参数中您
url
将$.ajax
指示/webservice.asmx/test
您可以在
data
其中指明您将发送到服务参数的信息。