gpt4 book ai didi

javascript - 如何使用 vb.net 代码从 javascript 调用 Wcf 服务?

转载 作者:行者123 更新时间:2023-11-28 02:11:23 27 4
gpt4 key购买 nike

如何从 vb.net 中的 javascript 调用 Wcf 服务?我收到错误“服务调用失败:415 不支持的媒体类型”。

我的网络服务代码:

<ServiceContract()> 
Public Class Service <OperationContract()> _
<WebInvoke(Method:="POST",
BodyStyle:=WebMessageBodyStyle.Wrapped, ResponseFormat:=WebMessageFormat.Json)> _
Public Function MyFunction(ByVal Count As String) As String
Return String.Format("Welcome in WCF call")
End Function
End Class

我的 HTML 代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!DOCTYPE html>
<title>Call WCF</title>
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
var counter = 0;
function CallMyService() {
// debugger;
//alert("hi");
counter++;

$.ajax({
type: "POST",
url: "http://localhost:25057/WCFVB/Service.svc/MyFunction",
data: '{"Count": "' + counter + '"}',
processData: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
// contentType: "application/json", // content type sent to server
success: ServiceSucceeded,
error: ServiceFailed
});
}

// ---- WCF Service call backs -------------------

function ServiceFailed(result) {
//debugger;
Log('Service call failed: ' + result.status + ' ' + result.statusText);
}

function ServiceSucceeded(result) {
// debugger;
var resultObject = result.MyFunctionResult;
Log("Success: " + resultObject);
}


function Log(msg) {
$("#logdiv").append(msg + "<br />");
}
</script>

</head>
<body>
<input id="Button1" type="button" value="Execute" onclick="CallMyService();" />

<div id="logdiv"></div>
</body>
</html>

最佳答案

这是针对我们的 WCF 服务运行的一个小 JavaScript 请求的示例 - 在本例中它只是一个简单的 ping 函数,但所有部分都在那里:

_________ 编辑____________ _

 <html> 
<head>
<meta http-equiv="Content-Type" content="text/xml; charset=UTF-8" />
<title>SOAP JavaScript Client Test</title>
<script type="text/javascript">

function Ping() {
//set up varable
var sContent;
// 'Content-Type: text/xml \r\n ' +
// 'Host: localhost:8085 \r\n \r\n ';
var s;
s+="<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
s+="<s:Body><HostConnect xmlns=\"http://SomeURL \">";
s+="<inMsg xmlns:a=\"http://schemas.datacontract.org/2004/07/ \" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
s+="<a:TextSection>" ;
s+="&lt;Method value=\"ping\" &gt;";
s+="</a:TextSection>";
s+="</inMsg>";
s+="</HostConnect>";
s+="</s:Body>";
s+="</s:Envelope>";


var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', Demo.URL.value, true);
//xmlhttp.overrideMimeType("text/xml; charset=UTF-8"); /* ... */

// alert(Demo.URL.value);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4||xmlhttp.readyState == 0) {
//alert("Ready state: " + xmlhttp.readyState.toString());
if (xmlhttp.status == 200) {
//alert("good");
Demo.pingresponse.value = "Response: " +xmlhttp.responseText;
}
if (xmlhttp.status !=200){
//alert("bad");
Demo.pingresponse.value = "Error: " +xmlhttp.status.toString() +" response text: " +xmlhttp.responseText;
}
} else {
//alert("readystate bad");
}
}
xmlhttp.setRequestHeader("POST http:servername:8085/HostInterface/HostConnect HTTP/1.1");
xmlhttp.setRequestHeader("VsDebuggerCausalityData","uIAA");
xmlhttp.setRequestHeader("SOAPAction","\"http://ServerName\"");
xmlhttp.setRequestHeader("Host","localhost:8085");
xmlhttp.setRequestHeader("Expect","100-continue");
xmlhttp.setRequestHeader("Accept-Encoding","gzip, deflate");
xmlhttp.setRequestHeader("Connection","Keep-Alive");
xmlhttp.setRequestHeader("Content-Length","639");
xmlhttp.setRequestHeader("Content-type", "text/xml; charset=utf-8");
xmlhttp.send(sContent);
}
</script>
</head>
<body>
<form name="Demo" action="" method="post">
<div>
Web Service URL
<input id="URL" type="text" size="140" value="" /><br />
<input type="button" value="Ping" onclick="Ping();" /><br />
<textarea id="pingresponse"cols="100" rows="10"></textarea> <br />
</div>
</form>
</body>
<html>

试试这个:

关于javascript - 如何使用 vb.net 代码从 javascript 调用 Wcf 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17039059/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com